1try(n) Tcl Built-In Commands try(n)
2
3
4
5______________________________________________________________________________
6
8 try - Trap and process errors and exceptions
9
11 try body ?handler...? ?finally script?
12______________________________________________________________________________
13
15 This command executes the script body and, depending on what the out‐
16 come of that script is (normal exit, error, or some other exceptional
17 result), runs a handler script to deal with the case. Once that has all
18 happened, if the finally clause is present, the script it includes will
19 be run and the result of the handler (or the body if no handler
20 matched) is allowed to continue to propagate. Note that the finally
21 clause is processed even if an error occurs and irrespective of which,
22 if any, handler is used.
23
24 The handler clauses are each expressed as several words, and must have
25 one of the following forms:
26
27 on code variableList script
28 This clause matches if the evaluation of body completed with the
29 exception code code. The code may be expressed as an integer or
30 one of the following literal words: ok, error, return, break, or
31 continue. Those literals correspond to the integers 0 through 4
32 respectively.
33
34 trap pattern variableList script
35 This clause matches if the evaluation of body resulted in an
36 error and the prefix of the -errorcode from the interpreter's
37 status dictionary is equal to the pattern. The number of prefix
38 words taken from the -errorcode is equal to the list-length of
39 pattern, and inter-word spaces are normalized in both the
40 -errorcode and pattern before comparison.
41
42 The variableList word in each handler is always interpreted as a list
43 of variable names. If the first word of the list is present and non-
44 empty, it names a variable into which the result of the evaluation of
45 body (from the main try) will be placed; this will contain the human-
46 readable form of any errors. If the second word of the list is present
47 and non-empty, it names a variable into which the options dictionary of
48 the interpreter at the moment of completion of execution of body will
49 be placed.
50
51 The script word of each handler is also always interpreted the same: as
52 a Tcl script to evaluate if the clause is matched. If script is a lit‐
53 eral “-” and the handler is not the last one, the script of the follow‐
54 ing handler is invoked instead (just like with the switch command).
55
56 Note that handler clauses are matched against in order, and that the
57 first matching one is always selected. At most one handler clause will
58 selected. As a consequence, an on error will mask any subsequent trap
59 in the try. Also note that on error is equivalent to trap {}.
60
61 If an exception (i.e. any non-ok result) occurs during the evaluation
62 of either the handler or the finally clause, the original exception's
63 status dictionary will be added to the new exception's status dictio‐
64 nary under the -during key.
65
67 Ensure that a file is closed no matter what:
68
69 set f [open /some/file/name a]
70 try {
71 puts $f "some message"
72 # ...
73 } finally {
74 close $f
75 }
76
77 Handle different reasons for a file to not be openable for reading:
78
79 try {
80 set f [open /some/file/name w]
81 } trap {POSIX EISDIR} {} {
82 puts "failed to open /some/file/name: it's a directory"
83 } trap {POSIX ENOENT} {} {
84 puts "failed to open /some/file/name: it doesn't exist"
85 }
86
88 catch(n), error(n), return(n), throw(n)
89
91 cleanup, error, exception, final, resource management
92
93
94
95Tcl 8.6 try(n)