Debian Codegraph @

doc/try.n

raw
1 '\"
2 '\" Copyright (c) 2008 Donal K. Fellows
3 '\"
4 '\" See the file "license.terms" for information on usage and redistribution
5 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
6 '\"
7 .TH try n 8.6 Tcl "Tcl Built-In Commands"
8 .so man.macros
9 .BS
10 '\" Note: do not modify the .SH NAME line immediately below!
11 .SH NAME
12 try \- Trap and process errors and exceptions
13 .SH SYNOPSIS
14 \fBtry\fI body\fR ?\fIhandler...\fR? ?\fBfinally\fI script\fR?
15 .BE
16 .SH DESCRIPTION
17 .PP
18 This command executes the script \fIbody\fR and, depending on what the outcome
19 of that script is (normal exit, error, or some other exceptional result), runs
20 a handler script to deal with the case. Once that has all happened, if the
21 \fBfinally\fR clause is present, the \fIscript\fR it includes will be run and
22 the result of the handler (or the \fIbody\fR if no handler matched) is allowed
23 to continue to propagate. Note that the \fBfinally\fR clause is processed even
24 if an error occurs and irrespective of which, if any, \fIhandler\fR is used.
25 .PP
26 The \fIhandler\fR clauses are each expressed as several words, and must have
27 one of the following forms:
28 .TP
29 \fBon \fIcode variableList script\fR
30 .
31 This clause matches if the evaluation of \fIbody\fR completed with the
32 exception code \fIcode\fR. The \fIcode\fR may be expressed as an integer or
33 one of the following literal words: \fBok\fR, \fBerror\fR, \fBreturn\fR,
34 \fBbreak\fR, or \fBcontinue\fR. Those literals correspond to the integers 0
35 through 4 respectively.
36 .TP
37 \fBtrap \fIpattern variableList script\fR
38 .
39 This clause matches if the evaluation of \fIbody\fR resulted in an error and
40 the prefix of the \fB\-errorcode\fR from the interpreter's status dictionary
41 is equal to the \fIpattern\fR. The number of prefix words taken from the
42 \fB\-errorcode\fR is equal to the list-length of \fIpattern\fR, and inter-word
43 spaces are normalized in both the \fB\-errorcode\fR and \fIpattern\fR before
44 comparison.
45 .PP
46 The \fIvariableList\fR word in each \fIhandler\fR is always interpreted as a
47 list of variable names. If the first word of the list is present and
48 non-empty, it names a variable into which the result of the evaluation of
49 \fIbody\fR (from the main \fBtry\fR) will be placed; this will contain the
50 human-readable form of any errors. If the second word of the list is present
51 and non-empty, it names a variable into which the options dictionary of the
52 interpreter at the moment of completion of execution of \fIbody\fR
53 will be placed.
54 .PP
55 The \fIscript\fR word of each \fIhandler\fR is also always interpreted the
56 same: as a Tcl script to evaluate if the clause is matched. If \fIscript\fR is
57 a literal
58 .QW \-
59 and the \fIhandler\fR is not the last one, the \fIscript\fR of the following
60 \fIhandler\fR is invoked instead (just like with the \fBswitch\fR command).
61 .PP
62 Note that \fIhandler\fR clauses are matched against in order, and that the
63 first matching one is always selected. At most one \fIhandler\fR clause will
64 selected. As a consequence, an \fBon error\fR will mask any subsequent
65 \fBtrap\fR in the \fBtry\fR. Also note that \fBon error\fR is equivalent to
66 \fBtrap {}\fR.
67 .PP
68 If an exception (i.e. any non-\fBok\fR result) occurs during the evaluation of
69 either the \fIhandler\fR or the \fBfinally\fR clause, the original exception's
70 status dictionary will be added to the new exception's status dictionary under
71 the \fB\-during\fR key.
72 .SH EXAMPLES
73 .PP
74 Ensure that a file is closed no matter what:
75 .PP
76 .CS
77 set f [open /some/file/name a]
78 \fBtry\fR {
79 puts $f "some message"
80 # ...
81 } \fBfinally\fR {
82 close $f
83 }
84 .CE
85 .PP
86 Handle different reasons for a file to not be openable for reading:
87 .PP
88 .CS
89 \fBtry\fR {
90 set f [open /some/file/name r]
91 } \fBtrap\fR {POSIX EISDIR} {} {
92 puts "failed to open /some/file/name: it's a directory"
93 } \fBtrap\fR {POSIX ENOENT} {} {
94 puts "failed to open /some/file/name: it doesn't exist"
95 }
96 .CE
97 .SH "SEE ALSO"
98 catch(n), error(n), return(n), throw(n)
99 .SH "KEYWORDS"
100 cleanup, error, exception, final, resource management
101 '\" Local Variables:
102 '\" mode: nroff
103 '\" End: