AssertionError not caught?

Steve Holden steve at holdenweb.com
Sun Feb 1 22:01:29 EST 2009


LX wrote:
> This one has me mystified good!
> 
> This works (print statement is executed as the Exception is caught) as
> advertised:
>     try:
>         raise AssertionError
>     except AssertionError:
>         print "caught AssertionError"
> 
> 
> But this one does not:
> 
>    def test():
>         raise AssertionError
> 
>     try:
>         test()
>     except AssertionError:
>         print "caught AssertionError"
> 
> other errors (e.g. IOError) work fine! This is on OSX 10.5.6, with the
> standard Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
> [GCC 4.0.1 (Apple Computer, Inc. build 5341)].
> 
> Is this a bug? Any hints / help would be much appreciated! Thank you.

This works for me under Cygwin:

>>> def test():
...     raise AssertionError
...
>>> try:
...     test()
... except AssertionError:
...     print "caught AssertionError"
...
caught AssertionError
>>>

I note from your indentation that it looks as though your code was
guarded by some other statement. Is it possible it wasn't executed at all?

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list