Error Handling In Tcl
NOT write exception handling code?What tools exist to aid in writing exception handling code?How exception handling workshv 2011-07-15: Here is a short snippet showing how to use the return command with -code error to throw an exceptionproc double x { if {![string is integer $x]} { return -code error "double expects an int, not '$x'" } return [expr {$x * 2}] } set x 5 puts "x = $x" puts "2x = [double $x]" set x Hello puts "x = $x" if {[catch {puts "2x = [double $x]"} errmsg]} { puts "Error: $errmsg" }Output:x = 5 2x = 10 x = Hello Error: double expects an int, not 'Hello'PYK 2016-07-11: For some time I was under the misapprehension that return -code error message was the "standard" way to throw an error. It's not. error and throw are the first commands to use for signalling an error. For one thing, error and throw report the line number they were called from. More importantly, return has the more general purpose of communicating information about some level to the interpreter. return can be considered a sort of restricted uplevel that provides a flexible mechanism for expressing various runtime conditions. return is usually considered to mean, "return from the current call", but what it actually means is "return to the interpreter some information about a level, which in the common case is that the interpreter should return from the current evaluation."DKF: The reason for using return -code error vs error or throw depends on where the error is. If the problem is in your code, use error or throw. If the problem is in your caller (e.g., because they gave you bad arguments) then use return -code error. Simple. See Also edittry ... finally ...throwcatchCategory Discussion HomeRecent changesHelpWhoAmI/LogoutCreate new pageRandom pagePrevious pageNext pageAdd commentsEditHistoryEdit summaryReferences Page contents SeeAlso Getting started What is Tcl? What is Tk? Getting Tcl/Tk Getting help Learning Tcl Tcl Dev Xchange About the Wiki Community Advocacy Conferences Chat, news, lists History Humor People Tcl websites Reference Companies Getting help Online books Online tutorials Manual page
a non-zero value). Some programs, like compilers, use this output channel not only to report errors but to report progress as well.A naive script like this: exec f77 -o myprog myprog.ffails (at least on Sun/Solaris), simply because the compiler, the now venerable Fortran 77 compiler, writes the names of the subroutines it encounters to standard-error.You can use catch to prevent the script from failing, but if there truly is an error in the compilation process, how do you know?Well, that is where the Tcl standard variables errorCode http://wiki.tcl.tk/17374 and errorInfo come in. They are described in extenso in the man page on tclvars. But here is an example: # Attempt to catch the return code from f77 # set rc [catch { exec f77 -c myff2.f } msg ] set errc $errorCode; set erri $errorInfo puts "rc: $rc" puts "errc: $errc" puts "erri: $erri" puts "msg: $msg"The file "myff2.f" does not http://wiki.tcl.tk/8489 exist and here is the result: rc: 1 errc: CHILDSTATUS 7612 1 erri: myff2.f: Error: Cannot open file myff2.f while executing "exec f77 -c myff2.f " msg: myff2.f: Error: Cannot open file myff2.fChanging the script (to compile the existing file "myff.f") gives: rc: 1 errc: NONE erri: myff.f: myff: while executing "exec f77 -c myff.f " msg: myff.f: myff:Note that the return value from catch is 1 in both cases, but the big difference is in errorCode.male 14th Sep. 2004:Again: the exit code of a piped executableOur problem is, that an external company provides an C executable (on Windows) using old FORTRAN functionality mapped in a DLL. This FORTRAN code returns failure code integers using 4Bytes (INTEGER*4). Theses failure codes are used as exit code of the C executable.Catching the close on the blocked command channel to the C executable let the exit code be stored in side the global errorCode variable.So far so good!Now the problem!On Windows the exit function in C allows to use a 4Byte integer (int or int32). But we get only the last byte of the exit code.example: the origi
Tcl - Basic Syntax Tcl - Commands Tcl - Data Types Tcl - Variables Tcl - Operators Tcl - Decisions Tcl - Loops Tcl - Arrays https://www.tutorialspoint.com/tcl-tk/tcl_error_handling.htm Tcl - Strings Tcl - Lists Tcl - Dictionary Tcl - Procedures Tcl - Packages Tcl - Namespaces Tcl - File I/O Tcl - Error Handling Tcl - Built-in Functions Tcl http://www.huecker.com/msw/tcl/tutorial/Tcl36.shtml - Regular Expressions Tk Tutorial Tk - Overview Tk - Environment Tk - Special Variables Tk - Widgets Overview Tk - Basic Widgets Tk - Layout Widgets Tk - Selection error handling Widgets Tk - Canvas Widgets Tk - Mega Widgets Tk - Fonts Tk - Images Tk - Events Tk - Windows Manager Tk - Geometry Manager Tcl/Tk Useful Resources Tcl/Tk - Quick Guide Tcl/Tk - Useful Resources Tcl/Tk - Discussion Selected Reading Developer's Best Practices Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary Who is Who Tcl - Error error handling in Handling Advertisements Previous Page Next Page Error handling in Tcl is provided with the help of error and catch commands. The syntax for each of these commands is shown below. Error syntax error message info code In the above error command syntax, message is the error message, info is set in the global variable errorInfo and code is set in the global variable errorCode. Catch Syntax catch script resultVarName In the above catch command syntax, script is the code to be executed, resultVarName is variable that holds the error or the result. The catch command returns 0 if there is no error, and 1 if there is an error. An example for simple error handling is shown below − #!/usr/bin/tclsh proc Div {a b} { if {$b == 0} { error "Error generated by error" "Info String for error" 401 } else { return [expr $a/$b] } } if {[catch {puts "Result = [Div 10 0]"} errmsg]} { puts "ErrorMsg: $errmsg" puts "ErrorCode: $errorCode" puts "ErrorInfo:\n$errorInfo\n" } if {[catch {puts "Result = [Div 10 2]"} errmsg]} { puts "ErrorMsg: $errmsg" puts "ErrorCode: $errorCo
. 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: -code the next value specifies the return status. Code must be one of: ok ........ Normal status return error ..... Proc returns error status return .... Normal retu