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
14

DESCRIPTION

16       The  catch  command may be used to prevent errors from aborting command
17       interpretation.  The catch command calls  the  Tcl  interpreter  recur‐
18       sively  to execute script, and always returns without raising an error,
19       regardless of any errors that might occur while executing script.
20
21       If script raises an error, catch will return a non-zero  integer  value
22       corresponding  to the exceptional return code returned by evaluation of
23       script.  Tcl defines the normal return code from script  evaluation  to
24       be  zero  (0),  or  TCL_OK.   Tcl  also defines four exceptional return
25       codes: 1 (TCL_ERROR), 2 (TCL_RETURN), 3 (TCL_BREAK),  and  4  (TCL_CON‐
26       TINUE).  Errors during evaluation of a script are indicated by a return
27       code of TCL_ERROR.  The other exceptional return codes are returned  by
28       the  return,  break,  and continue commands and in other special situa‐
29       tions as documented.  Tcl packages can define new commands that  return
30       other integer values as return codes as well, and scripts that make use
31       of the return -code command can also have return codes other  than  the
32       five defined by Tcl.
33
34       If  the  resultVarName argument is given, then the variable it names is
35       set to the result of the script evaluation.  When the return code  from
36       the  script  is  1 (TCL_ERROR), the value stored in resultVarName is an
37       error message.  When the return code from the script is 0 (TCL_OK), the
38       value stored in resultVarName is the value returned from script.
39
40       If  the optionsVarName argument is given, then the variable it names is │
41       set to a dictionary of return options returned by evaluation of script. │
42       Tcl  specifies  two  entries that are always defined in the dictionary: │
43       -code and -level.  When the return code from evaluation  of  script  is │
44       not  TCL_RETURN, the value of the -level entry will be 0, and the value │
45       of the -code entry will be the same as the return code.  Only when  the │
46       return  code  is  TCL_RETURN  will  the  values of the -level and -code 
47       entries be something else, as further described  in  the  documentation │
48       for the return command.                                                 │
49
50       When  the  return  code  from  evaluation of script is TCL_ERROR, three │
51       additional entries are defined in  the  dictionary  of  return  options │
52       stored  in optionsVarName: -errorinfo, -errorcode, and -errorline.  The │
53       value of the -errorinfo entry is a  formatted  stack  trace  containing │
54       more  information  about  the context in which the error happened.  The │
55       formatted stack trace is meant to be read by a person.   The  value  of │
56       the  -errorcode  entry is additional information about the error stored │
57       as a list.  The -errorcode value is meant to be  further  processed  by │
58       programs, and may not be particularly readable by people.  The value of │
59       the -errorline entry is an integer indicating which line of script  was │
60       being  evaluated when the error occurred.  The values of the -errorinfo 
61       and -errorcode entries of the most recent error are also  available  as │
62       values  of  the  global  variables  ::errorInfo and ::errorCode respec‐ │
63       tively.                                                                 │
64
65       Tcl packages may provide commands that set other entries in the dictio‐ │
66       nary  of  return options, and the return command may be used by scripts │
67       to set return options in addition to those defined above.
68

EXAMPLES

70       The catch command may be used in an if to branch based on  the  success
71       of a script.
72              if { [catch {open $someFile w} fid] } {
73                  puts stderr "Could not open $someFile for writing\n$fid"
74                  exit 1
75              }
76
77       There are more complex examples of catch usage in the documentation for
78       the return command.
79
80

SEE ALSO

82       break(n), continue(n), dict(n), error(n), return(n), tclvars(n)
83
84

KEYWORDS

86       catch, error
87
88
89
90Tcl                                   8.5                             catch(n)
Impressum