Exception Handling (again)

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

Exception Handling (again)

Günther Schmidt
Hi all,

I have this code but can't get it to work the way I want it.

[3 timesRepeat:
        [
                [ObjectHasChanged signal: 'error raised'.
                MessageBox notify: 'after signal']
                 on:ObjectHasChanged do:
                        [:ex | ex pass].
                 MessageBox notify: 'loop resumed']]

  on: ObjectHasChanged do:

        [:err | (MessageBox confirm: 'resume') ifTrue: [err resume]
ifFalse:[err return:''] ].


What I want to achieve is that I can cancel the execution right after
the error is raised (MessageBox notify: 'after signal') but have the
loop continue nevertheless.

When I press yes in the MessageBox the execution continues with a popup
("after signal") but when I press "no" the whole loop stops.

ObjectHasChanged is merely a subclass from Error with:

ObjectHasChanged>>isResumable

        ^true


What am I doing wrong?

Günther


Reply | Threaded
Open this post in threaded view
|

Re: Exception Handling (again)

Jochen Riekhof-6
> What I want to achieve is that I can cancel the execution right after
> the error is raised (MessageBox notify: 'after signal') but have the
> loop continue nevertheless.
>
> When I press yes in the MessageBox the execution continues with a popup
> ("after signal") but when I press "no" the whole loop stops.
Maybe you mean it like this:

3 timesRepeat:
     [[ObjectHasChanged signal: 'error raised'.
         MessageBox notify: 'after signal']
  on: ObjectHasChanged do:
     [:err | (MessageBox confirm: 'resume') ifTrue: [err resume]]].

Ciao

...Jochen


Reply | Threaded
Open this post in threaded view
|

Re: Exception Handling (again)

Günther Schmidt
Jochen,

thanks.

But no, I do not want to "handle" the exception in the loop itself, I
want to do that in code that "calls" the loop.

Jochen Riekhof schrieb:

>> What I want to achieve is that I can cancel the execution right after
>> the error is raised (MessageBox notify: 'after signal') but have the
>> loop continue nevertheless.
>>
>> When I press yes in the MessageBox the execution continues with a
>> popup ("after signal") but when I press "no" the whole loop stops.
> Maybe you mean it like this:
>
> 3 timesRepeat:
>     [[ObjectHasChanged signal: 'error raised'.
>         MessageBox notify: 'after signal']
>  on: ObjectHasChanged do:
>     [:err | (MessageBox confirm: 'resume') ifTrue: [err resume]]].
>
> Ciao
>
> ...Jochen
>


Reply | Threaded
Open this post in threaded view
|

Re: Exception Handling (again)

Jochen Riekhof-6
> But no, I do not want to "handle" the exception in the loop itself, I
> want to do that in code that "calls" the loop.

Hmm, I see, maybe like this:
[3 timesRepeat:
     [[ObjectHasChanged signal: 'error raised'.
         MessageBox notify: 'after signal']
  on: ObjectHasChanged do: [:err | err outer ifTrue: [err resume]]]
] on:  ObjectHasChanged do: [:err | err resume: (MessageBox confirm:
'resume')]

I /think/ in this case there is no other way but to pass back into the
loop the result of the confirmation somehow.

Ciao

...Jochen


Reply | Threaded
Open this post in threaded view
|

Re: Exception Handling (again)

Chris Uppal-3
Jochen, Günther,

> I /think/ in this case there is no other way but to pass back into the
> loop the result of the confirmation somehow.

A different approach would be to set a flag which causes the inner loop to
terminate itself explicitly, rather than by using nested exception handlers. Or
factoring the loop into a separate method so that you can exit from the middle
of it.  I'd strongly recommend looking for such a simpler solution.

Still, if you want complicated ;-) here's a totally different approach:

    3 timesRepeatWithExit: [:exit |
        [ObjectHasChanged signal: 'error raised'.
        MessageBox notify: 'after signal']
            on: ObjectHasChanged
            do: [:err | (MessageBox confirm: 'resume')
                                ifFalse: [exit value: false]].
    ].

where Integer>>timesRepeatWithExit: is defined as:

==================
    timesRepeatWithExit: a1Block
         ^ self timesRepeat: [a1Block value: [:result | ^ result]].
==================

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: Exception Handling (again)

Jochen Riekhof-7
Hi Chir...

> A different approach would be to set a flag which causes the inner loop to
> terminate itself explicitly, rather than by using nested exception
> handlers.
Yes, was my first idea - but I liked to find out if it was also possible
without this :-).

> Or factoring the loop into a separate method so that you can exit from the
> middle
> of it.  I'd strongly recommend looking for such a simpler solution.

Fully agreed!

Ciao

...Jochen