Exception handler chaining
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.
participants (3)
-
James Foster -
mlntdrv@gmail.com -
Richard Sargent