AssertionError not caught?

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Feb 1 21:55:01 EST 2009


LX <lxkain at gmail.com> writes:

> This works (print statement is executed as the Exception is caught)
> as advertised:

You don't actually show us the output you get.

>     try:
>         raise AssertionError
>     except AssertionError:
>         print "caught AssertionError"

    >>> try:
    ...     raise AssertionError
    ... except AssertionError:
    ...     print "caught AssertionError"
    ...
    caught AssertionError

> But this one does not:
> 
>    def test():
>         raise AssertionError
> 
>     try:
>         test()
>     except AssertionError:
>         print "caught AssertionError"

    >>> def test():
    ...     raise AssertionError
    ...
    >>> try:
    ...     test()
    ... except AssertionError:
    ...     print "caught AssertionError"
    ... 
    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)].

    Python 2.5.2 (r252:60911, Jan  4 2009, 22:51:23)
    [GCC 4.3.2] on linux2

> Is this a bug?

It's much more likely to be a difference between the code you posted
here and the code you're actually running.

-- 
 \           “It ain't so much the things we don't know that get us in |
  `\    trouble. It's the things we know that ain't so.” —Artemus Ward |
_o__)                                       (1834-67), U.S. journalist |
Ben Finney



More information about the Python-list mailing list