This document needs to be updated for class based exceptions. It only describes the old instance based signals. Please read the class-documentation of the Exception class for more info.
Smalltalk provides a powerful and flexible mechanism to handle exceptions
and errors in a graceful way.
Errors raise a so called Signal, which can be handled by a handlerblock.
Especially with respect to instance creation, deletion and garbage collection,
Smalltalk exceptions are very clean and easy to use.
All error conditions can be trapped and reacted upon by signal handlers.
This includes situations which are fatal in other languages, such as
stack overflow or array index bounds violations.
The most interesting classes are:
Every exception condition is assigned a signal or an exception class, which is raised when that condition is encountered. Therefore, a signal object pr an exception class can be thought of a way to identify a particular exception. Classes which raise a signal (as opposed to an exception) in one of its methods, creates these signal objects at class initialization time and keeps them in class variables for later. These classes provide access methods for these in their class protocol.
Notice that most parts of the system have been changed to use the newer class-based
exception mechanism, in which the Exception class itself is used as signal.
The protocols and semantics are identical, so all of the following is valid both for
signal instances and Exception classes.
Typical use:
Number arithmeticSignal
Number divisionByZeroSignal
Object errorSignal
[ ...some computation ... ] on:aSignalOrExceptionClass do:[ ... handlerBlock ...]
aSignalOrExceptionClass handle:[ ... handlerBlock ...] do:[ ...some computation ... ]
aSignalOrExceptionClass catch:[ ...some computation ... ]
More details are found in the
Signal
and
Exception
class documentation.
[ ...some computation ... ]
on:ExceptionClass1,ExceptionClass1,...
do:[:ex | ... handlerBlock ...]
creator
to the ex argument.
More details are found in the
SignalSet
class documentation.
They understand the same protocol as signals and exception classes.
(ExceptionHandlerSet
on:Ex1 do:[:ex | ... handler for Ex1 ...];
on:Ex2 do:[:ex | ... handler for Ex2 ...];
...
handleDo:[
...
protected block
...
].
More details are found in the examples section of the
ExceptionhandlerSet
class documentation.
Exception
are created when a signal/exception is raised.
They keep some information on where and why the exception occured and are passed as argument
to the signal handler block. Within the handler block, execution can be continued in various ways,
via messages to the exception object.
[
... some computation
] on: aSignalOrExceptionClass do:[:ex |
... handler action
]
or:
aSignalOrExceptionClass
handle:[:ex |
... handler action
]
do:[
... some computation
]
aSignalOrExceptionClass handle:[:ex |
...
ex return
]
aSignalOrExceptionClass handle:[:ex |
...
ex restart
]
aSignalOrExceptionClass handle:[:ex |
...
ex resume
]
anException parameter
anException creator
anException errorString
anException handlerContext
anException suspendedContext
aSignalOrExceptionClass raise
aSignalOrExceptionClass raiseWith:parameter
More details are found in the
Exception
class documentation.
Copyright © 1996 Claus Gittinger Development & Consulting
<info@exept.de>