Catch and name an exception in Python 2.5 +

Thomas Jollans t at jollybox.de
Sat Aug 27 05:57:58 EDT 2011


On 27/08/11 05:45, Steven D'Aprano wrote:
> Thomas Jollans wrote:
> 
>> On 26/08/11 21:56, Steven D'Aprano wrote:
> 
>>> Is there any way to catch an exception and bind it to a name which will
>>> work across all Python versions from 2.5 onwards?
>>>
>>> I'm pretty sure there isn't, but I thought I'd ask just in case.
>>
>> It's not elegant, and I haven't actually tested this, but this should
>> work:
>>
>> try:
>>     ...
>> except (ValueError, KeyError):
>>     error = sys.exc_info()[2]
> 
> Great! Thanks for that, except I think you want to use [1], not [2].

Ah, yes. Of course.



More information about the Python-list mailing list