Exception Handling
Frag uses exception handling - like many other object-oriented languages - to deal with
error situations. In Frag all exceptions are instances of the class Exception (see
Section Exception Objects: Method Reference) or its subclasses.
All exceptions thrown internally by the Frag interpreter and the predefined objects
are (at the moment) of the type Exception.
A user can either throw an exception of type Exception or derive a subclass
of it, and throw an exception of this type.
All pre-defined exceptions are unnamed objects. Exceptions could be named objects,
but usually this does not make much sense, since exceptions should get automatically
garbage collected, once they are caught. Hence, in general, it is advisable
to throw user-defined exceptions as unnamed objects.
throw is used to throw user defined exceptions. It can be used at any point
in a Frag program. throw requires an instance of the class
Exception or one of its subclasses as its argument.
The following example shows a method that throws an exception:
Object create O
O method x {} {
throw [Exception create -msg "My Exception"]
}
The pre-defined method msg of Exception is used to set a
user-defined error message on an exception. Typically this
is used during creation of an exception, before the exception is
thrown using throw, as shown in the example above.
The syntax of throw is:
throw <exceptionObj>
The following example first creates a user defined Exception subclass and
then throws an exception of that type.
Object create MyExceptionType -superclasses Exception
MyExceptionType method setMyProperty {v} {
self set myProperty "MyExceptionType: $v"
}
MyExceptionType method getMyProperty {} {
self get myProperty
}
Object create O
O method x {} {
throw [MyExceptionType create -msg "My Exception" -setMyProperty "a value"]
}
Another example is: In the following code, in method x an exception with the message
O-EXCEPTION is explicitly thrown. Hence, when
the method x is invoked, the catch statement catches the exception and
the message that is printed is O-EXCEPTION.
Object create O
O method x {} {
set r3 1
throw [Exception create -msg "O-EXCEPTION"]
set k 3
}
O create o
set e [catch {o x}]
puts [$e getMsg]
catch is used for catching exceptions. It
executes the code given as an argument and catches runtime errors. catch
returns an exception object,
if an exception was caught, otherwise an empty string.
An error message and a more verbose error information are written
into the exception object, which can be read using the methods
of the class Exception (explained below). In particular,
getInfo is used to get the full error information about an exception,
like it is printed during errors that are not caught.
getMsg can be used to obtain the short error message that is typically
printed in the first line of the error information. This message can be set
using msg.
The syntax of catch is:
catch <code>
catch executes the code given as an argument and catches runtime errors. catch
returns an exception object,
if an exception was caught, otherwise an empty string.
An error message and a more
verbose error information are written into the exception object, which can be
read using the methods of the class Exception (explained below).
The following example catches the error in the invocation set x and returns the
exception object. The exception object is an unnamed object.
Hence the if-statement prints the content of the error message and error information.
Typically such an if-statement can be used to handle an exception, if it occurs.
if {[set e [catch {set x}]] != ""} {
puts "MSG=[$e getMsg]"
puts "INFO=[$e getInfo]"
}
The following example catches the error in the first example above, and uses getMsg
to print the error message:
Object create O
O method x {} {
throw [Exception create -msg "My Exception"]
}
O create o
set e1 [catch {o x}]
puts [$e1 getMsg]
The following example catches the error in the second example above, and uses getMsg
to print the error message, and getMyProperty to get the user-defined property value:
Object create MyExceptionType -superclasses Exception
MyExceptionType method setMyProperty {v} {
self set myProperty "MyExceptionType: $v"
}
MyExceptionType method getMyProperty {} {
self get myProperty
}
Object create O
O method x {} {
throw [MyExceptionType create -msg "My Exception" -setMyProperty "a value"]
}
O create o
set e1 [catch {o x}]
puts [$e1 getMsg]
puts [$e1 getMyProperty]
The result is:
My Exception
MyExceptionType: a value
The Exception class is the superclass of all exceptions in Frag and defines the
minimal interface that Frag exceptions must support. We have used it already in a number of examples before.
In this section, we give a method reference for exception objects.
<exceptionInstance> getInfo
getInfo is used to get the full error information about an exception,
like it is printed during errors that are not caught.
The following example catches the error in the invocation set x and returns the
exception object. The exception object is an unnamed object. getInfo is used to
print the error information.
if {[set e [catch {set x}]] != ""} {
puts [$e getInfo]
}
The result is:
wrong number of arguments in 'set x', should be: 'set <var> <value>'
in: 'set x'
line = 1
at:
<exceptionInstance> getMsg
getMsg can be used to obtain the short error message that is typically
printed in the first line of the error information. This message can be set
using msg.
The following example catches the error in the invocation set x and returns the
exception object. The exception object is an unnamed object. getMsg is used to
print the error message.
if {[set e [catch {set x}]] != ""} {
puts [$e getMsg]
}
The result is:
wrong number of arguments in 'set x', should be: 'set <var> <value>'
<exceptionInstance> msg <value>
msg is used to set a user-defined error message on an exception. Typically this
is used during creation of an exception, before the exception is thrown using throw.
The following example shows a method that throws an exception, which gets a message
My Exception set using msg. Then the exception is caught and the error message
is printed. That is, My Exception is printed as an exception error message.
Object create O
O method x {} {
throw [Exception create -msg "My Exception"]
}
O create o
set e1 [catch {o x}]
puts [$e1 getMsg]