Why doesn't sys.excepthook work?
Asked Answered
R

1

9

Why isn't the sys.excepthook function called if I try to execute this code?

import sys;
def MyExcepthook(ex_cls, ex, tb):

    print("Oops! There's an Error.\n");

    a=open("./ERR.txt","w"); #Fixed as suggested by unutbu BUT the problem is the same!
    a.write("Oops! There's an Error.\n");
    a.close();

sys.excepthook = MyExcepthook;

def main():
    print(1/0);

if (__name__=="__main__"):
    main();

Output:

Traceback (most recent call last):
  File "C:\Users\Path\to\my\python\file.py", line 13, in <module>
    main();
  File "C:\Users\Path\to\my\python\file.py", line 10, in main
    print(1/0);
ZeroDivisionError: division by zero

Expected Output (by print):

Oops! There's an Error.

and a new file (Err.txt) should be created (by open)

The print function doesn't show the text and the file is not created because the sys.excepthook function is not called - why?

-->EDIT My Problem is caused by a bug in idle-python 3.4 because now i tried to run the code by interpreter python (command line) and it works! this makes my Question useless if not to warn about this bug in idle-python 3.4 i'm sorry and thanks for your help!

[SOLUTION] if someone has my same problem => Try to Run your code by command line! and not from IDE.

Reflexion answered 25/8, 2014 at 22:0 Comment(1)
[offtop] You'd not use semicolons in python :)Clinkerbuilt
A
5

Your custom excepthook must not itself raise an exception:

a=open("./ERR.txt")   # opens the file in read mode

should be

a=open("./ERR.txt", 'w')  # open the file in write mode.

When the custom excepthook raises an exception, you should see something like

Oops! There's an Error.

Error in sys.excepthook:
...
IOError: [Errno 2] No such file or directory: './ERR.txt'

Original exception was:
...
ZeroDivisionError: integer division or modulo by zero

PS. Don't forget to delete all those unnecessary semicolons!

import sys
def my_excepthook(ex_cls, ex, tb):
    msg = "Oops! There's an Error.\n"
    print(msg)

    with open("./ERR.txt", 'w') as a:
        a.write(msg)

sys.excepthook = my_excepthook

def main():
    print(1/0)

if __name__=="__main__":
    main()
Anglo answered 25/8, 2014 at 22:4 Comment(8)
This looks like the issue indeed. I'd further suggest using a with statement, rather than manually calling close on the file.Concurrent
I apologize for my inaccuracy! I fixed the args of open() but the problem remains the same! it does not works!Reflexion
@NerdEngine: "inaccuracy" - as in, the code you've posted doesn't accurately reflect the code you actually ran? If so, please copy-paste the actual code you ran to get your error into your question. Do not retype the code into the question box, and do not correct single inconsistencies as you notice them. Otherwise, we'll be here a lot longer than necessary, and you're a lot less likely to get the problem solved.Demy
@Demy I apologize again for my inaccuracy I will be much more alert from now. but now the code is correct!Reflexion
The code you have now displays "Oops! There's an Error." on my system. Maybe you should accept this answer :-)Roslynrosmarin
Strange! What operating system and which version of python? Did you try copy-pasting the code from your current question and running that?Roslynrosmarin
@MichaelClerx OS=win7 Python 3.4.1 IDE=idle-python3.4Reflexion
@MichaelClerx i'm sorry you were right! there's a bug in idle-python 3.4 because now i tried to run the code by interpreter python (command line) and it works! this makes my Question useless if not to warn about this bug in idle-python 3.4 i'm sorry and thanks for your help!Reflexion

© 2022 - 2024 — McMap. All rights reserved.