Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print info about exception in python 2.5?

Python 2.5 won't let me use this syntax:

try:
    code_that_raises_exception()
except Exception as e:
    print e
    raise

So how should I print information about an exception?

Thanks

EDIT: I'm writing a plugin for a program that includes kind of a pseudo python interpreter. It prints print statements but doesn't show exceptions at all.

like image 451
jefdaj Avatar asked Sep 28 '10 00:09

jefdaj


People also ask

How do I print exception details in Python?

If you are going to print the exception, it is better to use print(repr(e)) ; the base Exception. __str__ implementation only returns the exception message, not the type. Or, use the traceback module, which has methods for printing the current exception, formatted, or the full traceback.

How do I print an exception 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 I see exceptions in Python?

The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program's response to any exceptions in the preceding try clause.

How do I get exception to text in Python?

The most common method to catch and print the exception message in Python is by using except and try statement. You can also save its error message using this method. Another method is to use logger.


2 Answers

the 'as' keyword is a python 3 (introduced in 2.6) addition, you need to use a comma:

try:
    code_that_raises_exception()
except Exception, e:
    print e
    raise
like image 186
Mike Axiak Avatar answered Oct 16 '22 23:10

Mike Axiak


try:
  codethatraises()
except Exception, e:
  print e
  raise

not as easy to read as the latest and greatest syntax, but identical semantics.

like image 34
Alex Martelli Avatar answered Oct 16 '22 23:10

Alex Martelli