Proper way of handling multiple exception sections

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Proper way of handling multiple exception sections

NorbertHartl
I’m thinking about what would be the proper/least annoying way of having multiple exception sections. If I have a code block that can emit two different exceptions is the only way doing it

[ [ ... ]
  on: Exception1
  do: [ … ] ]
    on: Exception2
    do: [ … ]

on:do: just calls a primitive that marks the context being an exception handler. What is called in the image when the stack is scanned to find an exception handler?

thanks,

Norbert
Reply | Threaded
Open this post in threaded view
|

Re: Proper way of handling multiple exception sections

Igor Stasenko



On 25 October 2013 13:34, Norbert Hartl <[hidden email]> wrote:
I’m thinking about what would be the proper/least annoying way of having multiple exception sections. If I have a code block that can emit two different exceptions is the only way doing it

[ [ ... ]
  on: Exception1
  do: [ … ] ]
    on: Exception2
    do: [ … ]

on:do: just calls a primitive that marks the context being an exception handler. What is called in the image when the stack is scanned to find an exception handler?

Look at MethodContext exception handling category.
Not sure if this answer can satisfies you, because the code there is hard to read and hard to debug.
 
thanks,

Norbert



--
Best regards,
Igor Stasenko.
Reply | Threaded
Open this post in threaded view
|

Re: Proper way of handling multiple exception sections

camille teruel
In reply to this post by NorbertHartl

On 25 oct. 2013, at 13:34, Norbert Hartl <[hidden email]> wrote:

> I’m thinking about what would be the proper/least annoying way of having multiple exception sections. If I have a code block that can emit two different exceptions is the only way doing it
>
> [ [ ... ]
>  on: Exception1
>  do: [ … ] ]
>    on: Exception2
>    do: [ … ]

You can handle multiple exceptions like that:

[ ... ] on: Exception1, Exception2 do: [ :err | ... ]

But there is only one handling block.
If you need to have two different handling blocks, I once though of a solution where you would write things like that:

[ ... ] exceptionHandler
        on: Exception1 do: [ ... ];
        on: Exception2 do: [ ... ];
        execute.

The #exceptionHandler message would answer an ExceptionHandler on the block that would be able to manage multiple handling blocks.
Something like that:

ExceptionHandler >> on: anExceptionClass do: aBlock
        handlingBlock at: anExceptionClass put: aBlock

ExceptionHandler>> handlingBlockFor: anExceptionClass
        anExceptionClass ifNil: [ ^ nil ].
        ^ handlingBlock
                at: anExceptionClass
                ifAbsent: [ self handlingBlockFor: anExceptionClass superclass ]

ExceptionHandler >>#execute
        block on: Error do: [ :error |
                (self handlingBlockFor: error class)
                        ifNil: [ error pass ]
                        ifNotNil: [ :handlingBlock | handlingBlock value ] ]

> on:do: just calls a primitive that marks the context being an exception handler. What is called in the image when the stack is scanned to find an exception handler?

I think its ContextPart>>#findNextHandlerContextStarting

>
> thanks,
>
> Norbert