[Python-3000] self-contained exceptions

Collin Winter collinw at gmail.com
Wed Jan 10 18:46:31 CET 2007


On 1/10/07, Josiah Carlson <jcarlson at uci.edu> wrote:
> According to the translation rules Colin has previously provided...

Phillip came up the translation, I just implemented it.

>     re = get_resource()
>     ...
>     try:
>         something()
>     except RareException as re:
>         ...
>     ...
>     re.use_resource()
>
> is translated into...
>
>     re = get_resource()
>     ...
>     try:
>         try:
>             something()
>         except RareException:
>             re = <current exception>
>             ...
>     finally:
>         re = None
>         del re
>     ...
>     re.use_resource()
>
> That is, the 're = None; del re' stuff is executed regardless of whether
> the except body is executed.  The user would be notified right away.

The correct translation is

    re = get_resource()
    ...
    try:
        something()
    except RareException as re:
         try:
             ...
         finally:
             re = None
             del re
    ...
    re.use_resource()


So 're = None; del re;' is *only* executed if the except body is executed.

Collin Winter


More information about the Python-3000 mailing list