sys.exc_info

Thomas Jollans tjol at tjol.eu
Thu Jun 29 14:19:28 EDT 2017


On 29/06/17 08:50, Steven D'Aprano wrote:
> sys.exc_info() returns three items:
> 
> (exception type, exception value, traceback)
> 
> https://docs.python.org/2/library/sys.html#sys.exc_info
> 
> https://docs.python.org/3/library/sys.html#sys.exc_info
> 
> 
> 
> and may be used something like this example:
> 
> 
> try:
>     something
> except:
>     exc_type, exc, tb = sys.exc_info()
>     print(traceback.extract_tb(tb))
>     raise
> 
> 
> 
> Why does it return the exception type separately from the exception, when 
> the type can be derived by calling `type(exc)`?
> 
> 


Ah, Python history.

Back in the old days, it was possible to raise strings instead of the
classes that took over later.

Python 2.4.6 (#1, Jun 29 2017, 19:23:06)
[GCC 5.4.0 20160609] on linux4
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
...     raise 'error', 'message'
... except:
...     import sys
...     print sys.exc_info()
...
('error', 'message', <traceback object at 0x7fe297171128>)
>>>

This was deprecated in 2.5, and (I think?) removed in 2.7.

>From looking at the old documentation, I have a suspicion that until
Python 1.4 (or even earlier than that) the exception "value" was
actually never an exception instance. (I'm afraid I can't find a way to
install 1.4 and test it on my computer. At least not a reasonable one.)


Thomas




More information about the Python-list mailing list