use str

try:
    some_method()
except Exception as e:
    s = str(e)

Also, most exception classes will have an args attribute. Often, args[0] will be an error message.

It should be noted that just using str will return an empty string if there's no error message whereas using repr as pyfunc recommends will at least display the class of the exception. My take is that if you're printing it out, it's for an end user that doesn't care what the class is and just wants an error message.

It really depends on the class of exception that you are dealing with and how it is instantiated. Did you have something in particular in mind?

Answer from aaronasterling on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.4 documentation
Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to handle ...
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.4 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any excep...
🌐
Tutorialspoint
tutorialspoint.com › python › python_exceptions.htm
Python - Exceptions Handling
Exception handling in Python refers to managing runtime errors that may occur during the execution of a program. In Python, exceptions are raised when errors or unexpected situations arise during program execution, such as division by zero, trying
🌐
freeCodeCamp
freecodecamp.org › news › error-handling-in-python-introduction
Error Handling in Python – try, except, else, & finally Explained with Code Examples
April 11, 2024 - Lines 4 and 5 open an except clause for any ZeroDivisionError and instruct the program to print a message should we try to divide anything by 0 · You likely notice the issue. My variable x has the value 0, and I am trying to divide 5 by x. The best mathematicians in the world can’t divide by 0, and neither can Python.
🌐
Programiz
programiz.com › python-programming › exception-handling
Python Exception Handling (With Examples)
Since exceptions abnormally terminate the execution of a program, it is important to handle exceptions. In Python, we use the try...except block to handle exceptions.
🌐
Sentry
blog.sentry.io › practical-tips-on-handling-errors-and-exceptions-in-python
Guide to Errors vs Exceptions in Python | Sentry
April 1, 2025 - An exception is a type of error that occurs during program execution, disrupting the normal flow of code. When an exception is raised, Python halts execution and creates an exception object containing details about what went wrong.
Find elsewhere
🌐
Real Python
realpython.com › ref › builtin-exceptions
Python’s Built-in Exceptions (Reference) – Real Python
When something goes wrong during program execution, Python raises (or “throws”) an appropriate exception, which can be “caught” and handled using try…except blocks.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exception-handling
Python Exception Handling - GeeksforGeeks
3 weeks ago - Python Exception Handling allows a program to gracefully handle unexpected events (like invalid input or missing files) without crashing.
🌐
GeeksforGeeks
geeksforgeeks.org › python › user-defined-exceptions-python-examples
User-defined Exceptions in Python with Examples - GeeksforGeeks
February 12, 2026 - When we create a custom exception, we subclass Python’s built-in Exception class (or a subclass like ValueError, TypeError, etc.).
Top answer
1 of 5
202

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 Exceptions 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.

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)
2 of 5
84

To improve on the answer provided by @artofwarfare, here is what I consider a neater way to check for the message attribute and print it or print the Exception object as a fallback.

try:
    pass 
except Exception as e:
    print getattr(e, 'message', repr(e))

The call to repr is optional, but I find it necessary in some use cases.


Update #1:

Following the comment by @MadPhysicist, here's a proof of why the call to repr might be necessary. Try running the following code in your interpreter:

try:
    raise Exception 
except Exception as e:
    print(getattr(e, 'message', repr(e)))
    print(getattr(e, 'message', str(e)))

The repr(e) line will print Exception() and the str(e) line will print an empty string.


Update #2:

Here is a demo with specifics for Python 2.7 and 3.5: https://gist.github.com/takwas/3b7a6edddef783f2abddffda1439f533

🌐
Openai
developers.openai.com › api › docs › guides › rate-limits
Rate limits | OpenAI API
4 days ago - The OpenAI Cookbook has a Python notebook that explains how to avoid rate limit errors, as well an example Python script for staying under rate limits while batch processing API requests.
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations
2 weeks ago - Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching.
🌐
Python Tutor
pythontutor.com › visualize.html
Python Tutor - Visualize Code Execution
Free online compiler and visual debugger for Python, Java, C, C++, and JavaScript. Step-by-step visualization with AI tutoring.
🌐
Read the Docs
beautiful-soup-4.readthedocs.io › en › latest
Beautiful Soup Documentation — Beautiful Soup 4.4.0 documentation
Tag.insert() is just like Tag.append(), except the new element doesn’t necessarily go at the end of its parent’s .contents. It’ll be inserted at whatever numeric position you say. It works just like .insert() on a Python list:
🌐
W3Schools
w3schools.com › python › python_ref_exceptions.asp
Python Built-in Exceptions
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
🌐
Real Python
realpython.com › python-raise-exception
Python's raise: Effectively Raising Exceptions in Your Code – Real Python
January 25, 2025 - But what is an exception? An exception represents an error or indicates that something is going wrong. Some programming languages, such as C, and Go, encourage you to return error codes, which you check.