[Python-Dev] Chained Exceptions

Guido van Rossum gvanrossum at gmail.com
Sat May 14 00:28:22 CEST 2005


[Brett C.]
> Maybe, but as long as caught exceptions get cleared that should be an issue.
> Would this be solved if, when an 'except' branch is exited, exceptions are
> cleared?  So, in the above example, once the 'pass' is hit in catchit() no
> exception is considered active any longer.  This could be done with a CLEAR_EXC
> opcode very easily inserted at the end of an 'except' branch by the compiler.

Sure, but that would be backwards incompatible. There's plenty of code
that expects sys.exc_info() to continue to return the caught exception
*outside* the except block. This is all incredibly tricky, to some
extent for backwards compatibility reasons (please read the source
code for maintaining the exc_info data!).

In Python 3000, I think we can get rid of sys.exc_info() altogether
once we place the traceback in the exception object as the 'traceback'
attribute: if you want this info, all you need is write

    except SomeException, err:
        # now type is err.__class__, value is err, and traceback is
err.traceback.

If you want to have this with an "except:" clause, you can just catch
'Exception' or perhaps 'BaseException'. This isn't possible in Python
2.x since there's no single base class.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list