Python exception message line

How to get exception message in Python properly

What is the best way to get exceptions’ messages from components of standard library in Python? I noticed that in some cases you can get it via message field like this:

try: pass except Exception as ex: print(ex.message) 
try: pass except socket.error as ex: print(ex) 

You’re conflating two different things — except Foo as bar: is the same as except Foo, bar: (except the former is newer, and will continue to work in 3.x), whether the error comes with a message attribute or not is separate.

5 Answers 5

If you look at the documentation for the built-in errors, you’ll see that most Exception classes assign their first argument as a message attribute. Not all of them do though.

Notably, EnvironmentError (with subclasses IOError and OSError ) has a first argument of errno , second of strerror . There is no message . strerror is roughly analogous to what would normally be a message .

More generally, subclasses of Exception can do whatever they want. They may or may not have a message attribute. Future built-in Exception s may not have a message attribute. Any Exception subclass imported from third-party libraries or user code may not have a message attribute.

I think the proper way of handling this is to identify the specific Exception subclasses you want to catch, and then catch only those instead of everything with an except Exception , then utilize whatever attributes that specific subclass defines however you want.

Читайте также:  Install sun java applet

If you must print something, I think that printing the caught Exception itself is most likely to do what you want, whether it has a message attribute or not.

You could also check for the message attribute if you wanted, like this, but I wouldn’t really suggest it as it just seems messy:

try: pass except Exception as e: # Just print(e) is cleaner and more likely what you want, # but if you insist on printing message specifically whenever possible. if hasattr(e, 'message'): print(e.message) else: print(e) 

Источник

How do I print an exception in Python?

print(e) on keyerrors seems to give only the key, but not the full exception message, which is less than helpful.

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.

@MartijnPieters the print(repr(e)) doesn’t give any stracktrace. The print_exc from traceback module (mentioned in the other answer) though works in this case.

@Hi-Angel: Where am I claiming that printing repr(e) would give the stack trace? I’m talking about the difference between str(e) and repr(e) , the latter includes more information that you would also see in the last line(s) of a traceback. I explicitly mention the traceback module in my comment.

The traceback module provides methods for formatting and printing exceptions and their tracebacks, e.g. this would print exception like the default handler does:

import traceback try: 1/0 except Exception: traceback.print_exc() 
Traceback (most recent call last): File "C:\scripts\divide_by_zero.py", line 4, in 1/0 ZeroDivisionError: division by zero 

is there some kind of get_error_message call that I can print with seeing as I’m using my own printing routine to add some other things.

This snipped does not use the captured exception object. Can you expand the code to use ‘ex’? — as in except Exception as ex: .

@aaronsteers it does use the captured exception; in an exception handler the current exception is available via the sys.exc_info() function and the traceback.print_exc() function gets it from there. You’d only ever need to pass in an exception explicitly when not handling an exception or when you want to show info based on a different exception.

Yes, I would sometimes like to hold onto the exception and print it later, when I’m no longer in the ‘except’ block.

In Python 2.6 or greater it’s a bit cleaner:

except Exception as e: print(e) 

In older versions it’s still quite readable:

except Exception, e: print e 

Python 3: logging

Instead of using the basic print() function, the more flexible logging module can be used to log the exception. The logging module offers a lot extra functionality, for example, logging messages.

  • into a given log file, or
  • with timestamps and additional information about where the logging happened.

For more information check out the official documentation.

Usage

Logging an exception can be done with the module-level function logging.exception() like so:

import logging try: 1/0 except BaseException: logging.exception("An exception was thrown!") 

Output

ERROR:root:An exception was thrown! Traceback (most recent call last): File ". /Desktop/test.py", line 4, in 1/0 ZeroDivisionError: division by zero 

Notes

  • the function logging.exception() should only be called from an exception handler
  • the logging module should not be used inside a logging handler to avoid a RecursionError (thanks @PrakharPandey)

Alternative log-levels

It’s also possible to log the exception with another log level but still show the exception details by using the keyword argument exc_info=True , like so:

logging.critical("An exception was thrown!", exc_info=True) logging.error ("An exception was thrown!", exc_info=True) logging.warning ("An exception was thrown!", exc_info=True) logging.info ("An exception was thrown!", exc_info=True) logging.debug ("An exception was thrown!", exc_info=True) # or the general form logging.log(level, "An exception was thrown!", exc_info=True) 

Name and description only

Of course, if you don’t want the whole traceback but only some specific information (e.g., exception name and description), you can still use the logging module like so:

try: 1/0 except BaseException as exception: logging.warning(f"Exception Name: ") logging.warning(f"Exception Desc: ") 

Output

WARNING:root:Exception Name: ZeroDivisionError WARNING:root:Exception Desc: division by zero 

In this example, the exception is logged and the exception is «handled». One may wish to re-raise this exception, if not sufficiently handled.

(I was going to leave this as a comment on @jldupont’s answer, but I don’t have enough reputation.)

I’ve seen answers like @jldupont’s answer in other places as well. FWIW, I think it’s important to note that this:

except Exception as e: print(e) 

will print the error output to sys.stdout by default. A more appropriate approach to error handling in general would be:

except Exception as e: print(e, file=sys.stderr) 

(Note that you have to import sys for this to work.) This way, the error is printed to STDERR instead of STDOUT , which allows for the proper output parsing/redirection/etc. I understand that the question was strictly about ‘printing an error’, but it seems important to point out the best practice here rather than leave out this detail that could lead to non-standard code for anyone who doesn’t eventually learn better.

I haven’t used the traceback module as in Cat Plus Plus’s answer, and maybe that’s the best way, but I thought I’d throw this out there.

Источник

Оцените статью