Re-raise exception in sys.excepthook

I decided I wanted to implement an email function so that when an
unhandled exception occurs, it is emailed to myself. I've got this
working however, I want to re-raise the exception so the user knows that
an exception occurred.

I've got my except hook like below:

def MyExceptHook(type_, value, tb):
  ...code that does the emailing...
  
  #Reraise the exception so the user knows that an error occured
raise type_

sys.excepthook = MyExceptHook

Now, the exception does get raised, but the traceback looks a little
funny. My app already redirects stdout/stderr to a TextCtrl and here's
what I get:

Error in sys.excepthook:
Traceback (most recent call last):
  File "D:\Documents and
Settings\rickkylw\Desktop\Scripts\WellExplorer\WellExplorer.pyw", line
11923, in MyExceptHook
    raise type_
NameError

Original exception was:
Traceback (most recent call last):
  File "D:\Documents and
Settings\rickkylw\Desktop\Scripts\WellExplorer\WellExplorer.pyw", line
5268, in OnBugReport
    frame = BugReportDialog(self, "Submit a Bug Report")
  File "D:\Documents and
Settings\rickkylw\Desktop\Scripts\WellExplorer\WellExplorer.pyw", line
2368, in __init__
    print x
NameError: global name 'x' is not defined

But, the email that I get has this:

Traceback (most recent call last):
  File "D:\Documents and
Settings\rickkylw\Desktop\Scripts\WellExplorer\WellExplorer.pyw", line
5268, in OnBugReport
    frame = BugReportDialog(self, "Submit a Bug Report")
  File "D:\Documents and
Settings\rickkylw\Desktop\Scripts\WellExplorer\WellExplorer.pyw", line
2368, in __init__
    print x
NameError: global name 'x' is not defined

I intentionally put a statement I knew would raise an exception "print
x" in my class. Is there a way to raise the last exception such that it
looks like the text from the emailed version?

I know this is slightly OT, but I figured some of you gurus would know
:slight_smile:

-Kyle Rickey

How about raise value?

-Matthias

···

Am 15.04.2008, 21:33 Uhr, schrieb Rickey, Kyle W <Kyle.Rickey@bakerhughes.com>:

I decided I wanted to implement an email function so that when an
unhandled exception occurs, it is emailed to myself. I've got this
working however, I want to re-raise the exception so the user knows that
an exception occurred.

I've got my except hook like below:

def MyExceptHook(type_, value, tb):
  ...code that does the emailing...
  
  #Reraise the exception so the user knows that an error occured
raise type_

Matthias, thanks for your reply. Using `value` instead of `type_` give
me the same output but with a little more info in the last line of the
first part. From:

NameError

To:

NameError: global name 'x' is not defined

'raise type_, value' does the same as 'raise value'

-Kyle Rickey

···

-----Original Message-----
From: wxpython-users-bounces@lists.wxwidgets.org
[mailto:wxpython-users-bounces@lists.wxwidgets.org] On Behalf Of Nitro
Sent: Tuesday, April 15, 2008 2:40 PM
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Re-raise exception in sys.excepthook

Am 15.04.2008, 21:33 Uhr, schrieb Rickey, Kyle W <Kyle.Rickey@bakerhughes.com>:

I decided I wanted to implement an email function so that when an
unhandled exception occurs, it is emailed to myself. I've got this
working however, I want to re-raise the exception so the user knows

that

an exception occurred.

I've got my except hook like below:

def MyExceptHook(type_, value, tb):
  ...code that does the emailing...
  
  #Reraise the exception so the user knows that an error occured
raise type_

How about raise value?

-Matthias
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Matthias, thanks for your reply. Using `value` instead of `type_` give
me the same output but with a little more info in the last line of the
first part. From:

NameError

To:

NameError: global name 'x' is not defined

'raise type_, value' does the same as 'raise value'

Hmm, I get this:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.

def a(t,v,tb):

... raise v
...

import sys
sys.excepthook = a
1/0

Error in sys.excepthook:
Traceback (most recent call last):
   File "<stdin>", line 2, in a
ZeroDivisionError: integer division or modulo by zero

Original exception was:
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

You could always call

return sys.__excepthook__(type_, value, traceback)

after your mailing stuff I guess.

-Matthias

···

Am 15.04.2008, 21:49 Uhr, schrieb Rickey, Kyle W <Kyle.Rickey@bakerhughes.com>:

Ahh, so I guess it's not possible to avoid 'Error in sys.execepthook'
since I'm re-raising the exception there. If I put

return sys.__excepthook__(type_, value, traceback)

in my code, where does it return to?

-Kyle Rickey

···

-----Original Message-----
From: wxpython-users-bounces@lists.wxwidgets.org
[mailto:wxpython-users-bounces@lists.wxwidgets.org] On Behalf Of Nitro
Sent: Tuesday, April 15, 2008 2:55 PM
To: wxpython-users@lists.wxwidgets.org
Subject: Re: [wxpython-users] Re-raise exception in sys.excepthook

Am 15.04.2008, 21:49 Uhr, schrieb Rickey, Kyle W <Kyle.Rickey@bakerhughes.com>:

Matthias, thanks for your reply. Using `value` instead of `type_` give
me the same output but with a little more info in the last line of the
first part. From:

NameError

To:

NameError: global name 'x' is not defined

'raise type_, value' does the same as 'raise value'

Hmm, I get this:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.

def a(t,v,tb):

... raise v
...

import sys
sys.excepthook = a
1/0

Error in sys.excepthook:
Traceback (most recent call last):
   File "<stdin>", line 2, in a
ZeroDivisionError: integer division or modulo by zero

Original exception was:
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

You could always call

return sys.__excepthook__(type_, value, traceback)

after your mailing stuff I guess.

-Matthias
_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users