Envy opened on Exceptions

39 views
Skip to first unread message

Francesco Raymondi

unread,
Dec 13, 2013, 11:17:53 AM12/13/13
to va-sma...@googlegroups.com
Hi all,
I have a problem with exceptions: when I want to handle an exception like in the example below:


[1/0 ]
 when: ExAll
 do: [:signal | Transcript show: 'Error' ]


the Transcript shows the string 'Error' correctly, but the ENVY also opens (I attached a picture with ENVY)
Why?
What I mistake?

My configuration is:
VA ST 6.0.2 
WidgetKit 6.0.2
WindowBuilder Pro 6.0.2

Thank you very much.
Regards,

Francesco




2013_12_13_17_08_58_ENVY_Smalltalk_Debugger_on_13_12_2013_17_06_08_.png

Richard Sargent

unread,
Dec 13, 2013, 12:45:52 PM12/13/13
to va-sma...@googlegroups.com
>> the Transcript shows the string 'Error' correctly, but the *debugger* also opens

The problem is that you have not specified what to do with the exception. The default is to resume. Look at the "ANSI-API (signaledException)" category of Signal. You may want to use #return or #return: to provide a replacement value for the outcome of the failed block.

By the way, catching all exception is a potential weakness in a program, unless it is in the outermost part of execution (e.g., in a read and dispatch loop). Even then, it means your handler will be responding to Warnings and Notifications, not just Errors. You almost certainly will want to respond differently to resumable exceptions versus non-resumable ones.

Francesco Raymondi

unread,
Dec 16, 2013, 8:23:03 AM12/16/13
to va-sma...@googlegroups.com
Richard, I tried to do your solution but I'm not sure to have understood ...

Instead, I wrote something like this and it works:

[1/0]
 when: ExAll
 do: [:signal|  
Transcript show: (signal argument) . 
signal exitWith: [error]].

I have no idea if this solution will generate some problems or not.

Thank you for help.

Regards

Richard Sargent

unread,
Dec 16, 2013, 1:15:14 PM12/16/13
to VA Smalltalk
That is basically correct. The argument to #exitWith: (ANSI: #return:) should be consistent with what the guarded block should have returned. In your case, you have returned a Block evaluating some kind of variable named "error". Is that really what you wanted?

For example, the code that would have called 1/0 was expecting a number back, so typically the exception handler should return a number e.g. 0. Alternatively, but poorer, is to return nil or some other special value ... but that is a Java-like approach, so there are better ways along the lines of the #at:ifAbsent: methods.

Assuming the example you provided was in a method called #determineSomething, my preferred approach is to create a #determineSomethingOr: method which takes a Block for an argument. In the example you provided, you wouldn't tell the signal to exit, you would instead answer the value of the Block argument (e.g. ^aBlock value). This unwinds the stack, dropping the exception, etc.

Also, as I alluded before, always guard for the most specific exception rather than the most general, except in specific areas. In this example, the most specific exception would have been ZeroDivide.

[1/0]
   on: ZeroDivide
   do: [:signal | self logException: signal. signal return: 42 "or whatever :-) " ].

Note also that these days, #on:do: is preferred over #when:do:.



--
You received this message because you are subscribed to a topic in the Google Groups "VA Smalltalk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/va-smalltalk/y2iE1MKSY6c/unsubscribe.
To unsubscribe from this group and all its topics, send an email to va-smalltalk...@googlegroups.com.
To post to this group, send email to va-sma...@googlegroups.com.
Visit this group at http://groups.google.com/group/va-smalltalk.
For more options, visit https://groups.google.com/groups/opt_out.



--
Richard Sargent
Business Development Manager
503-766-4719
richard...@gemtalksystems.com
GemTalk Systems
15220 NW Greenbrier Parkway #240
Beaverton, OR 97006
Reply all
Reply to author
Forward
0 new messages