[Python-Dev] [Python-checkins] r88331 - in python/branches/py3k/Doc/howto: index.rst pyporting.rst

Nick Coghlan ncoghlan at gmail.com
Fri Feb 4 00:05:03 CET 2011


On Fri, Feb 4, 2011 at 8:01 AM, brett.cannon <python-checkins at python.org> wrote:
> +Capturing the Currently Raised Exception
> +----------------------------------------
> +One change between Python 2 and 3 that will require changing how you code is
> +accessing the currently raised exception.  In Python 2 the syntax to access the
> +current exception is::
> +
> +    try:
> +        raise Exception()
> +    except Exception, exc:
> +        # Current exception is 'exc'
> +        pass
> +
> +This syntax changed in Python 3 to::
> +
> +    try:
> +        raise Exception()
> +    except Exception as exc:
> +        # Current exception is 'exc'
> +        pass

Note that you can write it the Python 3 way in 2.6+ as well (this was
new syntax, so there weren't any backwards compatibility issues with
adding it). You only need to do the sys.exc_info dance if you need to
support 2.5 or earlier.

Other notes:
- explicit relative imports work in 2.6+ without needing a future import
- absolute imports are the default in 2.7

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list