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
>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.
Sure
try:
a = 4 / 0
except:
print 'Ooops!'
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.
--
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
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
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)
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