Exception - how to ignore it ?

Alan Kennedy alanmk at hotmail.com
Tue Jun 3 17:00:51 CEST 2003


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




More information about the Python-list mailing list