How can an Exception pass over an "except" clause ?

Tijs tijs_news at artsoftonline.com
Wed May 30 12:22:04 EDT 2007


Nebur wrote:

> I'm using the contract.py library, running Python 2.4.4.
> 
> Now I'm confronted with the following exception backtrace:
>  (...)
>   File "/usr/lib/python2.4/site-packages/contract.py", line 1265, in
> _check_preconditions
>     p = f.__assert_pre
> AttributeError: 'function' object has no attribute '__assert_pre'
> 
> For my surprise, I found that the code of contract.py around line 1265
> looks like:
> 
> 1264: try:
> 1265:     p = f.__assert_pre
> 1266: except AttributeError:
> 1267:     pass
> 
> I'd expect line 1267 to "swallow" the AttributeError siliently. But
> the application stops with the above backtrace.
> Someone familiar enough with the Python innards ? How can one manage
> that an "except" seems to be ignored ?
> 
>  Ruben

The 'raise' in line 1271 re-raises the last error instead of the exception
in whose block it is called. 

This:
try:
    raise KeyError
except:
    try:
        raise IndexError
    except: pass
    raise

raises IndexError, not KeyError.

-- 

Regards,
Tijs



More information about the Python-list mailing list