1try(n)           Forward compatibility implementation of [try]          try(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       try - try - Trap and process errors and exceptions
9

SYNOPSIS

11       package require Tcl  8.5
12
13       package require try  ?1?
14
15       ::try body ?handler...? ?finally script?
16
17______________________________________________________________________________
18

DESCRIPTION

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 ex‐
28              ceptional result), runs a handler script to deal with the  case.
29              Once  that  has  all happened, if the finally clause is present,
30              the script it includes will be run and the result of the handler
31              (or  the  body  if no handler matched) is allowed to continue to
32              propagate. Note that the finally clause is processed even if  an
33              error occurs and irrespective of which, if any, handler is used.
34
35              The  handler  clauses  are  each expressed as several words, and
36              must have one of the following forms:
37
38              on code variableList script
39                     This clause matches if the evaluation of  body  completed
40                     with  the  exception code code. The code may be expressed
41                     as an integer or one of the following literal words:  ok,
42                     error,  return, break, or continue. Those literals corre‐
43                     spond to the integers 0 through 4 respectively.
44
45              trap pattern variableList script
46                     This clause matches if the evaluation of body resulted in
47                     an error and the prefix of the -errorcode from the inter‐
48                     preter's status dictionary is equal to the  pattern.  The
49                     number of prefix words taken from the -errorcode is equal
50                     to the list-length of pattern, and inter-word spaces  are
51                     normalized in both the -errorcode and pattern before com‐
52                     parison.
53
54                     The variableList word in each handler  is  always  inter‐
55                     preted  as a list of variable names. If the first word of
56                     the list is present and non-empty, it  names  a  variable
57                     into which the result of the evaluation of body (from the
58                     main try) will be placed; this will  contain  the  human-
59                     readable  form  of  any errors. If the second word of the
60                     list is present and non-empty, it names a  variable  into
61                     which  the  options  dictionary of the interpreter at the
62                     moment of completion of execution of body will be placed.
63
64                     The script word of each handler  is  also  always  inter‐
65                     preted  the  same:  as  a  Tcl  script to evaluate if the
66                     clause is matched. If script is a literal - and the  han‐
67                     dler  is  not  the  last one, the script of the following
68                     handler is invoked instead (just  like  with  the  switch
69                     command).
70
71                     Note  that  handler clauses are matched against in order,
72                     and that the first matching one is always  selected.   At
73                     most one handler clause will selected.  As a consequence,
74                     an on error will mask any subsequent  trap  in  the  try.
75                     Also note that on error is equivalent to trap {}.
76
77                     If  an  exception  (i.e. any non-ok result) occurs during
78                     the evaluation of  either  the  handler  or  the  finally
79                     clause,  the  original exception's status dictionary will
80                     be added to the new exception's status  dictionary  under
81                     the -during key.
82

EXAMPLES

84       Ensure that a file is closed no matter what:
85
86              set f [open /some/file/name a]
87              try {
88                  puts \$f "some message"
89                  # ...
90              } finally {
91                  close \$f
92              }
93
94
95       Handle different reasons for a file to not be openable for reading:
96
97              try {
98                  set f [open /some/file/name]
99              } trap {POSIX EISDIR} {} {
100                  puts "failed to open /some/file/name: it's a directory"
101              } trap {POSIX ENOENT} {} {
102                  puts "failed to open /some/file/name: it doesn't exist"
103              }
104
105

BUGS, IDEAS, FEEDBACK

107       This  document,  and the package it describes, will undoubtedly contain
108       bugs and other problems.  Please report such in the category try of the
109       Tcllib  Trackers  [http://core.tcl.tk/tcllib/reportlist].   Please also
110       report any ideas for enhancements  you  may  have  for  either  package
111       and/or documentation.
112
113       When proposing code changes, please provide unified diffs, i.e the out‐
114       put of diff -u.
115
116       Note further that  attachments  are  strongly  preferred  over  inlined
117       patches.  Attachments  can  be  made  by  going to the Edit form of the
118       ticket immediately after its creation, and  then  using  the  left-most
119       button in the secondary navigation bar.
120

SEE ALSO

122       catch(n), error(n), return(n), throw(n)
123

KEYWORDS

125       cleanup, error, exception, final, resource management
126

CATEGORY

128       Utility
129
131       Copyright (c) 2008 Donal K. Fellows, BSD licensed
132
133
134
135
136tcllib                                 1                                try(n)
Impressum