1

I am trying to override sys.excepthook exception handling. However, it simply not works.

This is the code I am running.

from sys import excepthook

excepthook = lambda type, exception, traceback : print('foo')

raise Exception('bar')

The following code calls my overriden method:

from sys import excepthook

excepthook = lambda type, exception, traceback : print('foo')

try:
    raise Exception('bar')
except Exception as e:
    excepthook(Exception, e, e._traceback_)

This code is useless due to the fact sys.excepthook should catch unhandled exceptions.

I`m aware many people is having issues such as mine, however, none of the issues helped me solve my problem.

why-doesnt-sys-excepthook-work

adding-function-to-sys-excepthook

sys-excepthook-vs-handled-exceptions

Thanks.

Vikto
  • 512
  • 1
  • 7
  • 19

1 Answers1

1

Got it.

The problem is in importing sysmodule.

import sys

sys.excepthook = lambda type, exception, traceback : print('foo')

And not this:

from sys import excepthook

It seems python does not know about excepthook function from sysso it declares a new variable called excepthook. That`s why it does not work as stated in docs.

Vikto
  • 512
  • 1
  • 7
  • 19