1catch(n)                     Tcl Built-In Commands                    catch(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       catch - Evaluate script and trap exceptional returns
9

SYNOPSIS

11       catch script ?resultVarName? ?optionsVarName?
12______________________________________________________________________________
13

DESCRIPTION

15       The  catch  command may be used to prevent errors from aborting command
16       interpretation.  The catch command calls  the  Tcl  interpreter  recur‐
17       sively  to execute script, and always returns without raising an error,
18       regardless of any errors that might occur while executing script.
19
20       If script raises an error, catch will return a non-zero  integer  value
21       corresponding  to the exceptional return code returned by evaluation of
22       script.  Tcl defines the normal return code from script  evaluation  to
23       be  zero  (0),  or  TCL_OK.   Tcl  also defines four exceptional return
24       codes: 1 (TCL_ERROR), 2 (TCL_RETURN), 3 (TCL_BREAK),  and  4  (TCL_CON‐
25       TINUE).  Errors during evaluation of a script are indicated by a return
26       code of TCL_ERROR.  The other exceptional return codes are returned  by
27       the  return,  break,  and continue commands and in other special situa‐
28       tions as documented.  Tcl packages can define new commands that  return
29       other integer values as return codes as well, and scripts that make use
30       of the return -code command can also have return codes other  than  the
31       five defined by Tcl.
32
33       If  the  resultVarName argument is given, then the variable it names is
34       set to the result of the script evaluation.  When the return code  from
35       the  script  is  1 (TCL_ERROR), the value stored in resultVarName is an
36       error message.  When the return code from the script is 0 (TCL_OK), the
37       value stored in resultVarName is the value returned from script.
38
39       If  the optionsVarName argument is given, then the variable it names is
40       set to a dictionary of return options returned by evaluation of script.
41       Tcl  specifies  two  entries that are always defined in the dictionary:
42       -code and -level.  When the return code from evaluation  of  script  is
43       not  TCL_RETURN, the value of the -level entry will be 0, and the value
44       of the -code entry will be the same as the return code.  Only when  the
45       return  code  is  TCL_RETURN  will  the  values of the -level and -code
46       entries be something else, as further described  in  the  documentation
47       for the return command.
48
49       When the return code from evaluation of script is TCL_ERROR, four addi‐
50       tional entries are defined in the dictionary of return  options  stored
51       in optionsVarName: -errorinfo, -errorcode, -errorline, and -errorstack. │
52       The value of the -errorinfo entry is a formatted stack trace containing
53       more  information  about  the context in which the error happened.  The
54       formatted stack trace is meant to be read by a person.   The  value  of
55       the  -errorcode  entry is additional information about the error stored
56       as a list.  The -errorcode value is meant to be  further  processed  by
57       programs, and may not be particularly readable by people.  The value of
58       the -errorline entry is an integer indicating which line of script  was
59       being  evaluated when the error occurred.  The value of the -errorstack 
60       entry is an even-sized list made of token-parameter  pairs  accumulated │
61       while  unwinding  the stack. The token may be “CALL”, in which case the │
62       parameter is a list made of the proc name and arguments at  the  corre‐ │
63       sponding  level;  or it may be “UP”, in which case the parameter is the │
64       relative level (as in uplevel) of the previous CALL. The  salient  dif‐ │
65       ferences with respect to -errorinfo are that:                           │
66
67       [1]                                                                     │
68              it  is  a  machine-readable  form that is amenable to processing │
69              with [foreach {tok prm} ...],                                    │
70
71       [2]                                                                     │
72              it contains the true (substituted) values passed  to  the  func‐ │
73              tions, instead of the static text of the calling sites, and      │
74
75       [3]                                                                     │
76              it  is  coarser-grained,  with  only one element per stack frame │
77              (like procs; no separate elements  for  foreach  constructs  for │
78              example).
79
80       The  values of the -errorinfo and -errorcode entries of the most recent
81       error are also available as values of the global variables  ::errorInfo
82       and  ::errorCode respectively.  The value of the -errorstack entry sur‐ │
83       faces as info errorstack.
84
85       Tcl packages may provide commands that set other entries in the dictio‐
86       nary  of  return options, and the return command may be used by scripts
87       to set return options in addition to those defined above.
88

EXAMPLES

90       The catch command may be used in an if to branch based on  the  success
91       of a script.
92
93              if { [catch {open $someFile w} fid] } {
94                  puts stderr "Could not open $someFile for writing\n$fid"
95                  exit 1
96              }
97
98       There are more complex examples of catch usage in the documentation for
99       the return command.
100

SEE ALSO

102       break(n), continue(n), dict(n), error(n),  errorCode(n),  errorInfo(n),
103       info(n), return(n)
104

KEYWORDS

106       catch, error, exception
107
108
109
110Tcl                                   8.5                             catch(n)
Impressum