Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle Arbitrary Exception, Print Default Exception Message

I have a program, a part of which executes a loop. During the execution of this loop, there are exceptions. Obviously, I would like my program to run without errors, but for the sake of progress, I would like the program to execute over the entire input and not stop when an exception is thrown. The easiest way to do this would be by implementing an except block.

However, when I do this, it excepts all exceptions and continues with the program and I never get to see the exception message, which I need in order to debug.

Is there a way to except any arbitrary exception and be able to print out the exception message in the except block?

like image 617
inspectorG4dget Avatar asked Jan 05 '10 11:01

inspectorG4dget


People also ask

How do I catch an exception and print message?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How do you capture error messages in Python?

message (at least in Python 2.7. 12). If you want to capture the error message, use str(e) , as in the other answers.

What is print exception?

Class PrintException encapsulates a printing-related error condition that occurred while using a Print Service instance. This base class furnishes only a string description of the error. Subclasses furnish more detailed information if applicable.


1 Answers

try:
    #stuff
except Exception as e:
    print e

The traceback module provides various functions for extracting more information from the exception object (e, above).

Source Errors and Exceptions

like image 91
James Avatar answered Oct 04 '22 06:10

James