What's the best way to wrap a whole script in try..except?

Hari Sekhon sekhon.hari at googlemail.com
Wed Jun 21 09:02:18 EDT 2006


Hari Sekhon wrote:
> Jon Ribbens wrote:
>> In article <mailman.7287.1150885369.27775.python-list at python.org>, Hari Sekhon wrote:
>>   
>>> I want to wrap a whole script in try ... except. What is the best way of 
>>> doing this?
>>>     
>>
>> You could do this maybe:
>>
>>   import sys
>>
>>   def excepthook(exc_type, exc_value, tb):
>>     import modules_needed_to_notify_exception
>>     ...
>>
>>   sys.excepthook = excepthook
>>
>>   import modules_needed_by_script
>>   ...
>>   
> having tested this more, I was worried about a recursive call to 
> exception should an exception be raised inside the function but 
> luckily python deals and give you a double traceback.
>
> batteries included indeed.
>
> Thanks again
>
> Hari

I see that the exceptionhook is handed 3 things, the name of the error, 
the explanation,  and the traceback object.

Does anybody know how I can get the linenumber of the error the way it 
does normally, since this is very useful info when debugging.
I think I need the traceback module but I can't see how to extract the 
extra info from the traceback object passed to excepthook.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060621/9e552e96/attachment.html>


More information about the Python-list mailing list