Powerful Exceptions: an Overview
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
Exceptions
- Really powerful
- Can be resumed, restarted, and signaled as new exception
- Two important classes:
- For more complete reference, read Deep into Pharo
What You Will Learn
- To raise and trap exceptions
- Some nice helper methods
API Overview
defaultAction
is executed when an exception occurs and it is not trapped
Convenient messages:
Catching Example
Example:
Signaling an Exception
To raise an exception:
- create an instance of exception
- send it messages
signal
or signal: aMessage
Signaling an Exception
Usually classes propose a shortcut
Testing That an Exception Occurs
SUnit offers should:raise:
and shouldnt:raise:
to check occurrence of exceptions.
Kinds of Exceptions
Error
: all errors (subscript, message not understood, division by zero
Halt
: to stop the execution (and get a debugger)
Notification
: non fatal exceptions (deprecation, warning, timedout)
UnhandledError
: when an error occurs and that it is not trapped
Exceptions are Real Objects
When you send an unknown message Point new strangeAndBizarre
Deprecation
To support API migration, Pharo uses deprecation
When the deprecation setting is on, a warning is raised when a deprecated method is executed
Deprecation Implementation Use
Create an instance of Deprecation
and signal it
Exception Sets
Or
A Nice Helper: ensure:
- How to ensure that an expression is always executed (even if the program fails before)?
[ doSomething ] ensure: [ alwaysExecuteThis ]
Another nice Helper ifCurtailed:
- How to ensure that an expression is executed only if the program fails or returns?
[ doSomething ] ifCurtailed: [ onProblem ]
Exception Lookup
- Each process has its own exception environment: an ordered list of active handlers
- Process starts with an empty list
[ aaaa ] on: Error do: [ bbb ]
adds Error,bbb
to the beginning of the list
- When an exception is signaled, the system sends a message to the first handler
- If the handler cannot handle the exception, the next one is asked
- If no handler can handle the exception, then the default action is performed
Handling Exception
Just for your information ;)
Within an handler [ aaa ] on: anExceptionClass do: [ anHandler ]
, we can:
- Return an alternative result for the protected block (
return:
)
- Retry the protected block or a different block (
retryUsing:
)
- Resume the protected block at the failure point (
resume:
)
- Pass the caught exception to the enclosing handler (
pass
)
- Resignal a different exception (
resignalAs:
)
Returning From an Exception
We return a different string on normal or notification
Resuming from Resumable Exception
Warning
, Notification
and subclasses are resumable
Notification signal
raises an exception
- exception is handled
resume:
restores the context and the value returned normally as if the notification did not occur
What You Should Know
- Exceptions are powerful in Pharo.
- Offer a simple API
Raising
Installing:
- Helpers
[ doSomething ] ensure: [ alwaysDoThis ]
[ doSomething ] ifCurtailed: [ onProblem ]
/