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.