Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print original exception in excepthook

I am setting the sys.excepthook so that I can log every exception that occurs. Instead of writing to a log, let's use the following example:

def excepthook(self, type_, value, traceback):
    print "\n"
    print type_
    print value
    print traceback
    print "\n"

sys.excepthook = self.excepthook

Now let's say I create a type error, like so:

print 3 + str(2)

Without being caught, this goes into the excepthook and properly prints out the 3 variables:

<type 'exceptions.TypeError'>
unsupported operand type(s) for +
<traceback object at 0x02BAE800>

What I would like to do, is have it ALSO print out the full Exception that was sent to the excepthook (so, in this case, a TypeException). In other words, I'd like it to also display the following information).

Traceback (most recent call last):
  File "testcidnelite.py", line 13, in <module>
    print 3 + str(2)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

If I add the following line:

raise

it will display the exception properly; however, it will also display an error with the term raise:

Error in sys.excepthook:
Traceback (most recent call last):
  File "C:\psi-test-automation\Selenium\TestMethods2.py", line 145, in  excepthook
    raise
TypeError: exceptions must be old-style classes or derived from BaseException, not NoneType 

Altering it to:

raise type_

Will print out the following error:

Error in sys.excepthook:
Traceback (most recent call last):
  File "C:\psi-test-automation\Selenium\TestMethods2.py", line 145, in excepthook
    raise type_
TypeError

Original exception was:
Traceback (most recent call last):
  File "testcidnelite.py", line 13, in <module>
    print 3 + str(2)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

I want it to print out only the 2nd chunk (the original exception). Is this possible?

like image 452
user2869231 Avatar asked Feb 25 '15 16:02

user2869231


People also ask

How do you print an entire exception in Python?

Method 2: By using print_exception() method. This method prints exception information and stack trace entries from traceback object tb to file. Parameters: This method accepts the following parameters: if tb argument is not None, it prints a header Traceback (most recent call last):

How do I get an exception message in Python?

repr(e) gives you the exception(and the message string); str(e) only gives the message string. As an alternative for logging exception you could use logger. exception(e) instead. It will log the exception with traceback at the same logging.

How do you get Stacktrace in Python?

Print Stack Trace in Python Using traceback Module The traceback. format_exc() method returns a string that contains the information about exception and stack trace entries from the traceback object. We can use the format_exc() method to print the stack trace with the try and except statements.

How does Python handle unhandled exceptions?

An unhandled exception displays an error message and the program suddenly crashes. To avoid such a scenario, there are two methods to handle Python exceptions: Try – This method catches the exceptions raised by the program. Raise – Triggers an exception manually using custom exceptions.


1 Answers

You can use Python's traceback module to format an exception.

from traceback import format_exception

def excepthook(self, type_, value, traceback):
    print format_exception(type_, value, traceback)

sys.excepthook = self.excepthook

Check out the official documentation for more information and examples.

like image 87
Rob Cutmore Avatar answered Sep 22 '22 23:09

Rob Cutmore