Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Exception - how to ignore it ?

2 views
Skip to first unread message

Helmut Jarausch

unread,
Jun 3, 2003, 3:40:42 AM6/3/03
to
Hi,

is it possible for an exception handler
to just e.g. print something and set some
global flags but then to have the script
continue just after the statement which
raised the exception?

Thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany

vivek kumar

unread,
Jun 3, 2003, 4:58:34 AM6/3/03
to


>From: "Helmut Jarausch" <jara...@igpm.rwth-aachen.de>
>Reply-To: jara...@igpm.rwth-aachen.de
>To: pytho...@python.org
>Subject: Exception - how to ignore it ?
>Date: Tue, 03 Jun 2003 09:40:42 +0200


>
>Hi,
>
>is it possible for an exception handler
>to just e.g. print something and set some
>global flags but then to have the script
>continue just after the statement which
>raised the exception?
>
>Thanks for a hint,
>
>Helmut Jarausch

If you know exactly where and which statement is raising exception or can
raise exception then :

try:
this statement [raises | can raise] exception...
catch Exception,e:
print "Exception : ",str(e)
errFlag=1
your script execution will continue from here...

Regards
Vivek Kumar

_________________________________________________________________
Reconnect with old pals. Relive the happy times.
http://www.batchmates.com/msn.asp With just one click.


Tyler Eaves

unread,
Jun 3, 2003, 8:51:49 AM6/3/03
to
Helmut Jarausch wrote:

Sure

try:
a = 4 / 0
except:
print 'Ooops!'

Helmut Jarausch

unread,
Jun 3, 2003, 9:52:06 AM6/3/03
to vivek kumar, Tyler Eaves
Thanks for your reply!

vivek kumar wrote:
>> is it possible for an exception handler
>> to just e.g. print something and set some
>> global flags but then to have the script
>> continue just after the statement which
>> raised the exception?
>>

> If you know exactly where and which statement is raising exception or

No, that was the question.

Say an exception is raised at many places and I don't want
to change the code. One more example would be a signal from
hardware floating underflow.


--

Alan Kennedy

unread,
Jun 3, 2003, 11:00:51 AM6/3/03
to
Helmut Jarausch:

>>> is it possible for an exception handler
>>> to just e.g. print something and set some
>>> global flags but then to have the script
>>> continue just after the statement which
>>> raised the exception?

vivek kumar wrote:
>> If you know exactly where and which statement is raising exception or

Helmut Jarausch:


> No, that was the question.
>
> Say an exception is raised at many places and I don't want
> to change the code.

Then I believe the answer is a definitive NO. The relevant section of the python
language reference is

http://www.python.org/doc/current/ref/exceptions.html

which says:

"Python uses the 'termination' model of error handling: an exception handler can
find out what happened and continue execution at an outer level, but it cannot
repair the cause of the error and retry the failing operation (except by
re-entering the offending piece of code from the top)."

"continuing execution at an outer level" also means that you can't resume to the
statement immediately after the one that caused the exception, i.e.

try:
x = 1/0
itIsNotPossibleForThisToBeExecuted('WithoutCodeRestructuring')
except ZeroDivisionError, zde:
print "Got an exception: %s" % str(zde)

So if you're not willing or able to restructure your code, then you're out of
luck :-(

regards,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan

Terry Reedy

unread,
Jun 3, 2003, 3:57:24 PM6/3/03
to

"Helmut Jarausch" <jara...@skynet.be> wrote in message
news:3EDCA806...@skynet.be...

> Say an exception is raised at many places and I don't want
> to change the code. One more example would be a signal from
> hardware floating underflow.

Perhaps you are looking for an interrupt handler rather than a
(Python) exception handler. (When interrupt handlers resume,
execution continues with the next instruction.) Such are hardware
specific and best written in assembler or machine-specific C. Whether
you could write an interrupt handler that usefully interacts with a
running Python program via the Python-C API is beyond my knowledge.

Terry J. Reedy


Steven Taschuk

unread,
Jun 3, 2003, 4:29:31 PM6/3/03
to
Quoth Helmut Jarausch:

> is it possible for an exception handler
> to just e.g. print something and set some
> global flags but then to have the script
> continue just after the statement which
> raised the exception?

As Alan said, no.

An alternative approach: let your code accept an error-handling
callback function to be invoked when a problem occurs. Such a
callback can set global flags and return control to your code, or
raise an exception, etc..

--
Steven Taschuk stas...@telusplanet.net
"What I find most baffling about that song is that it was not a hit."
-- Tony Dylan Davis (CKUA)

Scott David Daniels

unread,
Jun 3, 2003, 11:01:46 PM6/3/03
to
Helmut Jarausch:
>is it possible for an exception handler
>to just e.g. print something and set some
>global flags but then to have the script
>continue just after the statement which
>raised the exception?

I'd chime in on the "avoid the temptation" chorus, but they seem
in full voice.

Xerox Labs (PARC) had an implementation language name Mesa with
an exception system that allowed you to fiddle things and return
as well as ever return fro mthe exception. So many bugs
were associated with that feature that they had a style
note that said, basically, "don't do that." After a few years
of the style note, they looked through the codebase and found
(I am hazy here) either a small number of uses left, or a small
number of uses not easily rewritten. At least a quarter of those
remaing beasties had bugs in them. So, they rewrote them all and
pulled the language feature.

This is all by way of saying, "you aint gonna need it."

-Scott David Daniels
Scott....@Acm.Org

0 new messages