1try(n) Forward compatibility implementation of [try] try(n)
2
3
4
5______________________________________________________________________________
6
8 try - try - Trap and process errors and exceptions
9
11 package require Tcl 8.5
12
13 package require try ?1?
14
15 ::try body ?handler...? ?finally script?
16
17______________________________________________________________________________
18
20 This package provides a forward-compatibility implementation of Tcl
21 8.6's try/finally command (TIP 329), for Tcl 8.5. The code was directly
22 pulled from Tcl 8.6 revision ?, when try/finally was implemented as Tcl
23 procedure instead of in C.
24
25 ::try body ?handler...? ?finally script?
26 This command executes the script body and, depending on what the
27 outcome of that script is (normal exit, error, or some other
28 exceptional result), runs a handler script to deal with the
29 case. Once that has all happened, if the finally clause is
30 present, the script it includes will be run and the result of
31 the handler (or the body if no handler matched) is allowed to
32 continue to propagate. Note that the finally clause is processed
33 even if an error occurs and irrespective of which, if any, han‐
34 dler is used.
35
36 The handler clauses are each expressed as several words, and
37 must have one of the following forms:
38
39 on code variableList script
40 This clause matches if the evaluation of body completed
41 with the exception code code. The code may be expressed
42 as an integer or one of the following literal words: ok,
43 error, return, break, or continue. Those literals corre‐
44 spond to the integers 0 through 4 respectively.
45
46 trap pattern variableList script
47 This clause matches if the evaluation of body resulted in
48 an error and the prefix of the -errorcode from the inter‐
49 preter's status dictionary is equal to the pattern. The
50 number of prefix words taken from the -errorcode is equal
51 to the list-length of pattern, and inter-word spaces are
52 normalized in both the -errorcode and pattern before com‐
53 parison.
54
55 The variableList word in each handler is always inter‐
56 preted as a list of variable names. If the first word of
57 the list is present and non-empty, it names a variable
58 into which the result of the evaluation of body (from the
59 main try) will be placed; this will contain the human-
60 readable form of any errors. If the second word of the
61 list is present and non-empty, it names a variable into
62 which the options dictionary of the interpreter at the
63 moment of completion of execution of body will be placed.
64
65 The script word of each handler is also always inter‐
66 preted the same: as a Tcl script to evaluate if the
67 clause is matched. If script is a literal - and the han‐
68 dler is not the last one, the script of the following
69 handler is invoked instead (just like with the switch
70 command).
71
72 Note that handler clauses are matched against in order,
73 and that the first matching one is always selected. At
74 most one handler clause will selected. As a consequence,
75 an on error will mask any subsequent trap in the try.
76 Also note that on error is equivalent to trap {}.
77
78 If an exception (i.e. any non-ok result) occurs during
79 the evaluation of either the handler or the finally
80 clause, the original exception's status dictionary will
81 be added to the new exception's status dictionary under
82 the -during key.
83
85 Ensure that a file is closed no matter what:
86
87 set f [open /some/file/name a]
88 try {
89 puts \$f "some message"
90 # ...
91 } finally {
92 close \$f
93 }
94
95
96 Handle different reasons for a file to not be openable for reading:
97
98 try {
99 set f [open /some/file/name]
100 } trap {POSIX EISDIR} {} {
101 puts "failed to open /some/file/name: it's a directory"
102 } trap {POSIX ENOENT} {} {
103 puts "failed to open /some/file/name: it doesn't exist"
104 }
105
106
108 This document, and the package it describes, will undoubtedly contain
109 bugs and other problems. Please report such in the category try of the
110 Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. Please also
111 report any ideas for enhancements you may have for either package
112 and/or documentation.
113
114 When proposing code changes, please provide unified diffs, i.e the out‐
115 put of diff -u.
116
117 Note further that attachments are strongly preferred over inlined
118 patches. Attachments can be made by going to the Edit form of the
119 ticket immediately after its creation, and then using the left-most
120 button in the secondary navigation bar.
121
123 catch(n), error(n), return(n), throw(n)
124
126 cleanup, error, exception, final, resource management
127
129 Utility
130
132 Copyright (c) 2008 Donal K. Fellows, BSD licensed
133
134
135
136
137tcllib 1 try(n)