473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problems trying to hook own exception function to sys.excepthook

Hello,

I am new to Python.

I tried to hook my own ExceptionPrintingFunction sys.excepthook but it
does not work.

This is what I wrote:
-----------------------------------------------------------------------
import sys

def MyOwnExceptHook(typ, val, tb):
print "Inside my own hook"

sys.excepthook = MyOwnExceptHook

x = 1/0
-----------------------------------------------------------------------
This is what I get
-----------------------------------------------------------------------
Traceback (most recent call last):
File
"E:/Home/Programming/Python/TryProjects/ExceptHandling1/Except5.py",
line 8, in <module>
x = 1/0
ZeroDivisionError: integer division or modulo by zero
-----------------------------------------------------------------------

I never see "Inside my own hook" which tells me that the hook is not
being called. What I really want to test is to stop the exception from
propagating further and leave the program intact.

What am I doing wrong? Please let me know if there are any other newbie
groups that I should probably try in stead.

Thanks

Sami
Apr 6 '08 #1
3 2114
Sami wrote:
Hello,

I am new to Python.

I tried to hook my own ExceptionPrintingFunction sys.excepthook but it
does not work.

This is what I wrote:
-----------------------------------------------------------------------
import sys

def MyOwnExceptHook(typ, val, tb):
print "Inside my own hook"

sys.excepthook = MyOwnExceptHook

x = 1/0
-----------------------------------------------------------------------
This is what I get
-----------------------------------------------------------------------
Traceback (most recent call last):
File
"E:/Home/Programming/Python/TryProjects/ExceptHandling1/Except5.py",
line 8, in <module>
x = 1/0
ZeroDivisionError: integer division or modulo by zero
-----------------------------------------------------------------------

I never see "Inside my own hook" which tells me that the hook is not
being called. What I really want to test is to stop the exception from
propagating further and leave the program intact.

What am I doing wrong? Please let me know if there are any other newbie
groups that I should probably try in stead.
Are you running your code from within idle? It wraps your script in
something like

try:
# run script
except:
# show traceback

(Have a look at InteractiveInterpreter.runcode() in code.py if you are
interested in the actual code)

So your except hook never gets to see the exception and therefore won't be
invoked. Run your script from the commandline and you will see the
behaviour you expected.

Peter

Apr 6 '08 #2
Peter Otten wrote:
Sami wrote:
>Hello,

I am new to Python.

I tried to hook my own ExceptionPrintingFunction sys.excepthook but it
does not work.

This is what I wrote:
-----------------------------------------------------------------------
import sys

def MyOwnExceptHook(typ, val, tb):
print "Inside my own hook"

sys.excepthook = MyOwnExceptHook

x = 1/0
-----------------------------------------------------------------------
This is what I get
-----------------------------------------------------------------------
Traceback (most recent call last):
File
"E:/Home/Programming/Python/TryProjects/ExceptHandling1/Except5.py",
line 8, in <module>
x = 1/0
ZeroDivisionError: integer division or modulo by zero
-----------------------------------------------------------------------

I never see "Inside my own hook" which tells me that the hook is not
being called. What I really want to test is to stop the exception from
propagating further and leave the program intact.

What am I doing wrong? Please let me know if there are any other newbie
groups that I should probably try in stead.

Are you running your code from within idle? It wraps your script in
something like

try:
# run script
except:
# show traceback

(Have a look at InteractiveInterpreter.runcode() in code.py if you are
interested in the actual code)

So your except hook never gets to see the exception and therefore won't be
invoked. Run your script from the commandline and you will see the
behaviour you expected.

Peter
Great!! Thank you. I have been trying since yesterday to get to the
bottom of this.

Does this mean that one should always use the command line to run a
python program?

Sami
Apr 6 '08 #3
Sami wrote:
Does this mean that one should always use the command line to run a
python program?
Yes.

Peter

Apr 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jon Perez | last post by:
(sorry for the duplicate post, just wanted to make the subject line clearer) How do you set up pdb such that you will automatically get dropped into its prompt if an unanticipated exception...
2
by: Kreedz | last post by:
Hi, I'm making some intelligent logging module that redirects stdout and stderr of a wxPython GUI. I am logging the exceptions in a wx TreeCtrl. My problem is that when an exception is thrown,...
6
by: Don | last post by:
I'm having problems working with a streamwriter object. After closing the streamwriter and setting it to Nothing, I try to delete the file it was writing to, but I always get the following error...
10
by: Steve | last post by:
Does any one know if python has the ability to run a shutdown hook. For example you set a method to run when the python process is shutting down, like it recieved a kill signal? Basically...
2
by: Mardy | last post by:
Hi all, I have written a slightly modified version of the CGIHTTPServer; the difference with the module included in the standard python distrubution is that, since I know that the CGI to be...
0
by: Pramod TK | last post by:
Hi, Sometimes during execution of python scripts below mentioned error string is displayed on the console. "Unhandled exception in thread started by Error in sys.excepthook: Original exception...
0
by: Nathan | last post by:
Hi folks! Throughout my python development career, I've occasionally made various developer tools to show more information about assertions or exceptions with less hassle to the programmer. ...
2
by: Sami | last post by:
Hello, In the Python book that I am using to learn the language it says that the traceback.print_exc() can be used to stop exception propagation and make the program keep running. Here is a...
0
by: PyNoob | last post by:
By default, uncaught exceptions stop the execution of python scripts (similar to what bash does when -e us set.) I need to change this behavior, so that the user gets informed about the uncaught...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.