Exception Handling
Chris Muller
afunkyobject at yahoo.com
Mon Mar 20 20:33:44 UTC 2006
If you wish to handle FooError and UnrelatedToFooError in the same way, you can use the concatenation operator:
[ someObject foo ]
on: FooError, UnrelatedToFooError
do: [ : err | "handle the err" ]
If, however, you wish to handle them differently you may nest them:
[ [ someObject foo ]
on: FooError
do: [ :fooErr | "handle fooErr" ] ]
on: UnrelatedToFooError
do: [ :unToFooErr | "..." ]
For this second case, I implemented a helper method on BlockContext that allows me to specify multiple handlers conveniently without nesting:
BlockContext>>#maOn: exc1 do: block1 on: exc2 do: block2
^ [
[ self value ]
on: exc1
do: block1 ]
on: exc2
do: block2
Hope this helps..
- Chris
----- Original Message ----
From: Zulq Alam <zulq at orange.net>
To: The general-purpose Squeak developers list <squeak-dev at lists.squeakfoundation.org>
Sent: Monday, March 20, 2006 2:12:48 PM
Subject: Exception Handling
Hello,
I'm a bit confused about exception handling, perhaps you can help please?
If I have a method #foo and #foo can signal two errors FooError and
UnrelatedToFooError how do I handle both errors using #on:do: without
having something like...
[someObject foo]
on: Error
do: [:error | (error class = FooError) ifTrue: ["do something
specific to FooError"].
(error class = UnrelatedToFooError) ifTrue: ["do something
specific to UnrelatedToFooError"]].
In Java I would do something like
try {
somObject.foo();
}
catch (FooError e) {
// do something specific to FooError
}
catch (UnrelatedToFooError e) {
// do something specific to UnrelatedToFooError
}
Even if under the hood the same is happening this seems to take care of
some very common code.
Is there something in Smalltalk/Squeak will provide something similar?
Thanks,
Zulq.
More information about the Squeak-dev
mailing list
|