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?
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):
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With