tcl 8.x in the namespace version
Martin Lemburg, Berlin (Germany)
eMail: | martin.lemburg@epost.de |
The author gives no warrenty on nothing bad caused by this package!
This package provides the capability to build control structures well known from C++ and Java.
This control structures allow to execute/evaluate code and catch exceptions occuring during the execution and to execute/evaluate code depending on the raised/catched exception.
To load this package ...
package require tcf ?1.0?
To catch a normal tcl/tk error is not difficult. You only have to use Syntax1 and Syntax2 below.
try { ... }
excecutes/evaluates the given block or command and catches any error or raised exception. The command try returns the evaluation result.
try { ... } finally { ... }
executes/evalutes the given block or command. If an error or exception occurs the block/command behind the keyword finally will be executed or evaluated.
The command try returns the result of the evaluated block or command or if an error occurs it will return the result of the finally block or command.
To catch an exception needs some preparations.
An exception could only be caught if its defined. A not defined exception causes the try-command to misbehave and to raise an error.
To use exceptions you have to use the second command "exceptions" inside this package.
This command has following options:
exceptions on | switches the all exceptions on to be caught if raised | |
---|---|---|
exceptions off | switches the all exceptions off to prevent catching | |
exceptions reset | deletes all defined exceptions and "lowers" the status (see below: lower) | |
exceptions read | reads exception definitions from a file, a list or a string | |
exceptions add | adds an exception | |
exceptions def | adds or changes an exception | |
exceptions set | adds or changes an exception | |
exceptions del | deletes an exception | |
exceptions undef | deletes an exception | |
exceptions unset | deletes an exception | |
exceptions clear | deletes all exceptions | |
exceptions enable | enables an exception to be caught if raised | |
exceptions disable | disables an exception to prevent to be catched | |
exceptions change | changes the definition of an exception | |
exceptions changecode | changes the exceptions code (like errorCode) | |
exceptions changemessage | changes the exceptions message | |
exceptions info | returns all exception names matching a given pattern | |
exceptions exceptioninfo | returns the code and message of an exception | |
exceptions ison | returns if the exceptionhandling is turned on | |
exceptions isoff | returns if the exceptionhandling is turned off | |
exceptions current | returns the name of the last raised exception | |
exceptions lastraised | returns the name of the last raised exception | |
exceptions israised | returns true if the given exception is raised | |
exceptions exists | returns true if the given exception is defined | |
exceptions iscatchable | returns true if the given exception is enabled | |
exceptions catchnraise | executes a given block/command and raises an exception if the execution fails. If the exception is not defined yet, it will be defined inline | |
exceptions raise | raises the given exception.If the exception is not defined yet, it will be defined inline | |
exceptions lower | sets the exceptions status to "no exception raised" |
Syntax3 and Syntax4 describes the way to use the try-command with exceptions.
try { ... } catch exception { ... }
executes/evalutes the given block or command. If an error/exception occurs the block/command behind the keyword catch will be executed or evaluated if the raised exception matches the name of the exception given with the catch-keyword.
If no given exception matches the error/exception from inside the try block or command will be raised regulary and will not be catched!
The command try returns the result of the evaluated block/command or if an error occurs it will return the result of the finally block or command.
The syntax could be extended by several catch-divisions:
try { ... } catch exception1 { ... } catch exception2 { ... } catch exception3 { ... } ...
try { ... } catch exception { ... } catch ... { ... } finally { ... }
behaves like in Syntax3, but executes the finally block/command if no given exception matches to the raised exception.
The try-command could be used with the following two options:
-global
-level level
The option -global causes the try-command to execute/evaluate the given block or command in the global scope (level #0).
The option -level causes the try-command to execute/evaluate the given block or command in the given absolute or relative level.
Even the catch-blocks/commands or the finally-block/command are executed in demanded level/scope.
An example is in the subdirectory "sample" of the directory of this document. Start this example script "sample.tcl" using wish or tclsh.
try manualpage, exceptions manualpage