:TITLE: Debugging & Errors - errorInfo errorCode catch error return ;# ;# RCSID: $Header: /cvsroot/tcl/tcltutorial/original/Tcl36.lsn,v 1.1 2004/11/04 16:01:14 davidw Exp $ ;# Copyright (c) 1995 Clif Flynt ;# 9300 Fleming Rd. ;# Dexter, MI 48130 ;# clif@cflynt.com ;# See file "NOTICE" for licensing terms. ;# :LESSON_TEXT_START_LEVEL 0: Tcl commands and procs return both a status and value. A command or proc always returns a status, and may optionally return a value.

When a command executes correctly, the return status is TCL_OK. When an error occurs within a Tcl command, it returns TCL_ERROR instead of TCL_OK. When this occurs, the Tcl command that had the error places an informational string in the global variable errorInfo and returns a status of TCL_ERROR to the calling command. As the Tcl call stack unwinds, each Tcl command appends an informational message to the global variable errorInfo, and returns TCL_ERROR to the command above it.

This actually occurs when any exception condition occurs, including break and continue. Break and continue normally occur within a loop of some sort, and the loop command catches the exception and processes it properly.

Interpreted Tcl code can also catch exceptions. If a Tcl command is the argument to the catch command, any exception that the command generates is captured and returned. At this point the calling proc can decide how to handle the event.

error message ?info? ?code?
Generates an error condition and forces the Tcl call stack to unwind, with error information being added at each step.

If info or code are provided, the errorInfo and errorCode variables are initialized with these values.

catch script ?varName?
Evaluates and executes script. The return value of catch is the status return of the Tcl interpreter after it executes script If there are no errors in script, this value is TCL_OK. Otherwise it is an error value.

If varName is supplied, the value returned by script is placed in varName.

return ?-code code? ?-errorinfo info? ?-errorcode errorcode? ?value?
Generates a return exception condition. The possible arguments are:
-code
the next value specifies the return status. Code must be one of:
  • ok ........ Normal status return
  • error ..... Proc returns error status
  • return .... Normal return
  • break ..... Proc returns break status
  • continue .. Proc returns continue status
-errorinfo
info will be the first string in the errorInfo variable.
-errorcode
The proc will set errorCode to errorcode.
value
The string value will be the value returned by this proc.
errorInfo
errorInfo is a global variable that contains the error information from commands that have failed.
errorCode
errorCode is a global variable that contains the error code from command that failed.
:TEXT_END: :LESSON_TEXT_START_LEVEL 1: In previous lessons we discussed how the return command could be used to return a value from a proc. In Tcl, a proc may return a value, but it always returns a status.

When a command executes correctly, the return status is TCL_OK. When an error occurs within a Tcl command, it returns TCL_ERROR instead of TCL_OK. When this occurs, the Tcl command that had the error places an informational string in the global variable errorInfo and returns a status of TCL_ERROR to the calling command. As the Tcl call stack unwinds, each Tcl command appends an informational message to the global variable errorInfo, and returns TCL_ERROR to the command above it.

This actually occurs when any exception condition occurs, including break and continue. Break and continue normally occur within a loop of some sort, and the loop command catches the exception and processes it properly.

Interpreted Tcl code can also catch exceptions. If a Tcl command is the argument to the catch command, any exception that the command generates is captured and returned. At this point the calling proc can decide how to handle the event.

For example, if an open call returns an error, the user could be prompted to provide another file name.

A Tcl proc can also generate an error status condition. This can be done by specifying an error return with an option to the return command, or by using the error command. In either case, a message will be placed in errorInfo, and the proc will return a TLC_ERROR status.

error message ?info? ?code?
Generates an error condition and forces the Tcl call stack to unwind, with error information being added at each step.

If info or code are provided, the errorInfo and errorCode variables are initialized with these values.

catch script ?varName?
Evaluates and executes script. The return value of catch is the status return of the Tcl interpreter after it executes script If there are no errors in script, this value is TCL_OK. Otherwise it is an error value.

If varName is supplied, the value returned by script is placed in varName.

return ?-code code? ?-errorinfo info? ?-errorcode errorcode? ?value?
Generates a return exception condition. The possible arguments are:
-code
the next value specifies the return status. Code must be one of:
  • ok ........ Normal status return
  • error ..... Proc returns error status
  • return .... Normal return
  • break ..... Proc returns break status
  • continue .. Proc returns continue status
-errorinfo
info will be the first string in the errorInfo variable.
-errorcode
The proc will set errorCode to errorcode.
value
The string value will be the value returned by this proc.
errorInfo
errorInfo is a global variable that contains the error information from commands that have failed.
errorCode
errorCode is a global variable that contains the error code from command that failed.
:TEXT_END: :LESSON_TEXT_START_LEVEL 2: In previous lessons we discussed how the return command could be used to return a value from a proc, and how some commands (such as the string match command) return a success or failure code when they execute. This value that is explicitly returned is only one of the values that is returned. All Tcl commands also return a status when they execute. The status return tells whether the command executed correctly, or if an exception occurred while it was executing.

For example, when a string match command finds a match, it returns a 1 for its return value. It also returns TCL_OK as its status. If there were no match, string match would return a 0, but the status would still be TCL_OK. However, if string match were called with only one argument, it would return an error string, and the status returned would be TCL_ERROR.

When an error occurs within a Tcl command, it returns TCL_ERROR instead of TCL_OK. When this occurs, the Tcl command that had the error places an informational string in the global variable errorInfo and returns a status of TCL_ERROR to the calling command. As the Tcl call stack unwinds, each Tcl command appends an informational message to the global variable errorInfo, and returns TCL_ERROR to the command above it.

This actually occurs when any exception condition occurs, including break and continue. Break and continue normally occur within a loop of some sort, and the loop command catches the exception and processes it properly.

Interpreted Tcl code can also catch exceptions. If a Tcl command is the argument to the catch command, any exception that the command generates is captured and returned. At this point the calling proc can decide how to handle the event.

For example, if an open call returns an error, the user could be prompted to provide another file name.

A Tcl proc can also generate an error status condition. This can be done by specifying an error return with an option to the return command, or by using the error command. In either case, a message will be placed in errorInfo, and the proc will return a TLC_ERROR status.

The example code shows the catch command being used several times. All of the procs in this example will generate an exception. The catch command is used to catch that exception. After the exception has been caught, errorInfo and errorCode are reported.

Run the example, and examine the output. You'll see that errorInfo is always set if an error occurs, but errorCode may not be set. The last example, trying to open a non-existent file, does set errorCode.

error message ?info? ?code?
Generates an error condition and forces the Tcl call stack to unwind, with error information being added at each step.

If info or code are provided, the errorInfo and errorCode variables are initialized with these values.

catch script ?varName?
Evaluates and executes script. The return value of catch is the status return of the Tcl interpreter after it executes script If there are no errors in script, this value is TCL_OK. Otherwise it is an error value.

If varName is supplied, the value returned by script is placed in varName.

return ?-code code? ?-errorinfo info? ?-errorcode errorcode? ?value?
Generates a return exception condition. The possible arguments are:
-code
the next value specifies the return status. Code must be one of:
  • ok ........ Normal status return
  • error ..... Proc returns error status
  • return .... Normal return
  • break ..... Proc returns break status
  • continue .. Proc returns continue status
-errorinfo
info will be the first string in the errorInfo variable.
-errorcode
The proc will set errorCode to errorcode.
value
The string value will be the value returned by this proc.
errorInfo
errorInfo is a global variable that contains the error information from commands that have failed.
errorCode
errorCode is a global variable that contains the error code from command that failed.
:TEXT_END: :CODE_START: proc errorproc {x} { if {$x > 0} { error "Error generated by error" "Info String for error" $x } } catch errorproc puts "after bad proc call: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" set errorInfo ""; catch {errorproc 0} puts "after proc call with no error: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" catch {errorproc 2} puts "after error generated in proc: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" proc returnErr { x } { return -code error -errorinfo "Return Generates This" -errorcode "-999" } catch {returnErr 2} puts "after proc that uses return to generate an error: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" proc withError {x} { set x $a } catch {withError 2} puts "after proc with an error: ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" catch {open "/no_such_directory/no_such_file" "r"} puts "after an error call to a nonexistent file:" puts "ErrorCode: $errorCode" puts "ERRORINFO:\n$errorInfo\n" :TEXT_END: