tcf (Try Catch Finally) manual


REQUIREMENTS

tcl 8.x in the namespace version

AUTHOR

Martin Lemburg, Berlin (Germany)

eMail:martin.lemburg@epost.de

WARRENTY

The author gives no warrenty on nothing bad caused by this package!

PURPOSE

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?

NORMAL USAGE

To catch a normal tcl/tk error is not difficult. You only have to use Syntax1 and Syntax2 below.

Syntax 1:
try {
    ...
}

excecutes/evaluates the given block or command and catches any error or raised exception. The command try returns the evaluation result.

Syntax 2:
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.

EXTENDED USAGE

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 onswitches the all exceptions on to be caught if raised
exceptions offswitches the all exceptions off to prevent catching
exceptions resetdeletes all defined exceptions and "lowers" the status (see below: lower)
exceptions readreads exception definitions from a file, a list or a string
exceptions addadds an exception
exceptions defadds or changes an exception
exceptions setadds or changes an exception
exceptions deldeletes an exception
exceptions undefdeletes an exception
exceptions unsetdeletes an exception
exceptions cleardeletes all exceptions
exceptions enableenables an exception to be caught if raised
exceptions disabledisables an exception to prevent to be catched
exceptions changechanges the definition of an exception
exceptions changecodechanges the exceptions code (like errorCode)
exceptions changemessagechanges the exceptions message
exceptions inforeturns all exception names matching a given pattern
exceptions exceptioninforeturns the code and message of an exception
exceptions isonreturns if the exceptionhandling is turned on
exceptions isoffreturns if the exceptionhandling is turned off
exceptions currentreturns the name of the last raised exception
exceptions lastraisedreturns the name of the last raised exception
exceptions israisedreturns true if the given exception is raised
exceptions existsreturns true if the given exception is defined
exceptions iscatchablereturns true if the given exception is enabled
exceptions catchnraiseexecutes 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 raiseraises the given exception.If the exception is not defined yet, it will be defined inline
exceptions lowersets the exceptions status to "no exception raised"

Syntax3 and Syntax4 describes the way to use the try-command with exceptions.

Syntax 3:
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 {
    ...
} ...

Syntax 4:
try {
    ...
} catch exception {
    ...
} catch ... {
    ...
} finally {
    ...
}

behaves like in Syntax3, but executes the finally block/command if no given exception matches to the raised exception.

ENHANCED USAGE

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.

EXAMPLE

An example is in the subdirectory "sample" of the directory of this document. Start this example script "sample.tcl" using wish or tclsh.

LINKS

try manualpage, exceptions manualpage


bug descriptions, comments and ideas to: martin.lemburg@epost.de - December 1999