In other languages there is the possibility to chain exception handlers like this:
try { doOne(); doTwo(); doThree(); }
catch(ExceptionOne ex){
handleOne();
}
catch(ExceptionTwo ex) {
handleTwo();
}
catch(ExceptionThree ex) {
handleThree();
}
catch(Exception ex) {
handleRest();
}
Is this possible in Pharo? I’ve tried
[ block ]
on: ExceptionOne do: [ handleOne ]
on: ExceptionTwo do: [ handleTwo ]
but it is invalid syntax.
The #’on:do:’ implementation in Block accepts either an Exception or an ExceptionSet as the first parameter. So you can do something like the following (I’m typing from memory without trying so may have syntax errrors):
[ “tryBlock” ] on: (Array with: ExceptionOne with: ExceptionTwo) do: [:ex |
(ex isKindOf: ExceptionOne) ifTrue: [ “handleOne” ].
(ex isKindOf: ExceptionTwo) ifTrue: [ “handleTwo” ].
].
The above code can be improved in a variety of ways, but it should get you started. Note also that since Smalltalk allows you to modify base classes, you could add #’on:do:on:do:’ to Block.
James
On May 25, 2023, at 8:24 AM, mlntdrv@gmail.com wrote:
In other languages there is the possibility to chain exception handlers like this:
try { doOne(); doTwo(); doThree(); }
catch(ExceptionOne ex){
handleOne();
}
catch(ExceptionTwo ex) {
handleTwo();
}
catch(ExceptionThree ex) {
handleThree();
}
catch(Exception ex) {
handleRest();
}
Is this possible in Pharo? I’ve tried
[ block ]
on: ExceptionOne do: [ handleOne ]
on: ExceptionTwo do: [ handleTwo ]
but it is invalid syntax.
Rather than ‘Array with:with:’ it probably should be ‘ExceptionSet with:with:’.
On May 25, 2023, at 9:19 AM, James Foster via Pharo-users pharo-users@lists.pharo.org wrote:
The #’on:do:’ implementation in Block accepts either an Exception or an ExceptionSet as the first parameter. So you can do something like the following (I’m typing from memory without trying so may have syntax errrors):
[ “tryBlock” ] on: (Array with: ExceptionOne with: ExceptionTwo) do: [:ex |
(ex isKindOf: ExceptionOne) ifTrue: [ “handleOne” ].
(ex isKindOf: ExceptionTwo) ifTrue: [ “handleTwo” ].
].
The above code can be improved in a variety of ways, but it should get you started. Note also that since Smalltalk allows you to modify base classes, you could add #’on:do:on:do:’ to Block.
James
On May 25, 2023, at 8:24 AM, mlntdrv@gmail.com wrote:
In other languages there is the possibility to chain exception handlers like this:
try { doOne(); doTwo(); doThree(); }
catch(ExceptionOne ex){
handleOne();
}
catch(ExceptionTwo ex) {
handleTwo();
}
catch(ExceptionThree ex) {
handleThree();
}
catch(Exception ex) {
handleRest();
}
Is this possible in Pharo? I’ve tried
[ block ]
on: ExceptionOne do: [ handleOne ]
on: ExceptionTwo do: [ handleTwo ]
but it is invalid syntax.
#, will do the trick.
ExceptionOne, ExceptionTwo
Nesting the #on:do: messages is probably the more correct approach, since
each one would be catching just one family of exceptions and not require
class testing. Unfortunately, nested #on:do: messages are terribly ugly.
Many people (I hate that phrase!) tend to isolate the protected
implementation from the protecting code.
e.g. the implementation of #doSomething wraps a message send to
#doSomethingProtected in the exception handlers.
On Thu, May 25, 2023 at 9:45 AM James Foster via Pharo-users <
pharo-users@lists.pharo.org> wrote:
Rather than ‘Array with:with:’ it probably should be ‘ExceptionSet
with:with:’.
On May 25, 2023, at 9:19 AM, James Foster via Pharo-users <
pharo-users@lists.pharo.org> wrote:
The #’on:do:’ implementation in Block accepts either an Exception or an
ExceptionSet as the first parameter. So you can do something like the
following (I’m typing from memory without trying so may have syntax
errrors):
[ “tryBlock” ] on: (Array with: ExceptionOne with: ExceptionTwo) do: [:ex
|
(ex isKindOf: ExceptionOne) ifTrue: [ “handleOne” ].
(ex isKindOf: ExceptionTwo) ifTrue: [ “handleTwo” ].
].
The above code can be improved in a variety of ways, but it should get you
started. Note also that since Smalltalk allows you to modify base classes,
you could add #’on:do:on:do:’ to Block.
James
On May 25, 2023, at 8:24 AM, mlntdrv@gmail.com wrote:
In other languages there is the possibility to chain exception handlers
like this:
try { doOne(); doTwo(); doThree(); }
catch(ExceptionOne ex){
handleOne();
}
catch(ExceptionTwo ex) {
handleTwo();
}
catch(ExceptionThree ex) {
handleThree();
}
catch(Exception ex) {
handleRest();
}
Is this possible in Pharo? I’ve tried
[ block ]
on: ExceptionOne do: [ handleOne ]
on: ExceptionTwo do: [ handleTwo ]
but it is invalid syntax.