: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.
If info or code are provided, the errorInfo and errorCode
variables are initialized with these values.
If varName is supplied, the value returned by script is
placed in varName.
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.
If info or code are provided, the errorInfo and errorCode
variables are initialized with these values.
If varName is supplied, the value returned by script is
placed in varName.
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.
If info or code are provided, the errorInfo and errorCode
variables are initialized with these values.
If varName is supplied, the value returned by script is
placed in varName.