Today's Smalltalk Daily looks at using Exception Handlers in Smalltalk.
The Examples used in the screencast:
"simple exception handling"
answer := [4/0]
on: ZeroDivide
do: [:ex | ex returnWith: 0].
"a simple exception"
aString := 'This is a String'.
aString at: 3 put: $f.
"now do a retry"
[aString at: 3 put: $f]
on: NoModificationError
do: [:ex | aString beMutable.
ex retry].
^aString
"Handle more than one"
content := [HttpClient new get: 'http://www.cincomsmalltalk.com']
on: HttpException, OSErrorHolder peerFaultSignal, OSErrorHolder transferFaultSignal, OSErrorHolder operationStartedSignal
do: [:ex | ex returnWith: nil].
"create the set of exceptions"
toHandle := ExceptionSet new.
toHandle
add: HttpException;
add: OSErrorHolder peerFaultSignal;
add: OSErrorHolder transferFaultSignal;
add: OSErrorHolder operationStartedSignal.
^toHandle
To watch, click on the viewer below:
If you have trouble viewing that directly, you can click here to download the video directly
You can also watch it on YouTube:
Technorati Tags:
exception handler, error handler, smalltalk