Home Akazien Verlag   
Lesezeichen [ Info # QR-Code # NLP Training ]Fr 18 Aug 2017 10:32:21 # Grundlagen der Programmierung | Tcl Tutorial.


. Debugging & Errors - errorInfo errorCode catch error return .

[ Previous | Index | Next ]

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:
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
info will be the first string in the errorInfo variable.
The proc will set errorCode to errorcode.
The string value will be the value returned by this proc.
Global variable that contains the error information from commands that have failed.
Global variable that contains the error code from command that failed.


. Example .

   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"

[ Home | Top ]
[ . Previous | Index | Next . ]
Der Inhalt dieser Seite wurde am 24.05.2016 um 12.17 Uhr aktualisiert.
Navigation Seminare Magic Software Projekte Publikationen Kontakt Home
   huecker dot com * Germany
© 1999, 2017 Franz-Josef Hücker. All Rights Reserved.
Send Page Print Page LinkedIn follow me on twitter RSS Feeds & Podcasts