For Python 2.6 and later and Python 3.x:

except Exception as e: print(e)

For Python 2.5 and earlier, use:

except Exception,e: print str(e)
Answer from jldupont on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org › news › python-print-exception-how-to-try-except-print-an-error
Python Print Exception – How to Try-Except-Print an Error
March 15, 2023 - In fact, you can handle all the exceptions in Python with try…except. For example, if you have a large program and you don’t know whether an identifier exists or not, you can execute what you want to do with the identifier in a try block and handle a possible error in the except block: try: print("Here's variable x:", x) except: print("An error occured") # An error occured
Discussions

Print exception notes - in repr(exc) or otherwise - Ideas - Discussions on Python.org
Exception notes are printed in tracebacks, but they are omitted when an exception is handled and logged or printed. I think a possibly helpful information is lost. # Python 3.11+ enote = ValueError("error message") enot… More on discuss.python.org
🌐 discuss.python.org
0
September 4, 2024
How would I get my error message to print before Python errors?
That error is a python2 error. The easiest fix is to use python3 instead. More on reddit.com
🌐 r/learnpython
11
7
January 1, 2021
Try/Except isn't printing my message
Except is used for catching exceptions, but your code does not appear to throw any exceptions. I believe instead of the try and except, you want to use an else:. More on reddit.com
🌐 r/learnpython
8
7
August 18, 2023
How to get the error line?
I want to ask in general when I execute small code from the terminal I get the error but I dont know which line cause this error so how I know which line ? Thanks More on discuss.python.org
🌐 discuss.python.org
12
0
February 9, 2024
People also ask

What does it mean by "print an exception" in Python?
Printing an exception in Python refers to displaying exact records approximately errors that arise in the course of the execution of a program. These facts include the form of the exception, an error message, and often a traceback showing the sequence of calls that caused the mistake.
🌐
taglineinfotech.com
taglineinfotech.com › home › how do i print an exception in python?
How do I Print an Exception in Python? - Tagline Infotech
Why is printing exceptions crucial?
Printing exceptions is essential for debugging. It affords insights into what went wrong within the code, supporting developers in discovering and dealing with errors correctly. The records printed enable an understanding of the character and context of the exception.
🌐
taglineinfotech.com
taglineinfotech.com › home › how do i print an exception in python?
How do I Print an Exception in Python? - Tagline Infotech
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-print-exception
Python Print Exception - GeeksforGeeks
July 23, 2025 - Explanation: This code tries to convert the string "text" into an integer, which isn’t possible and raises a ValueError. The except block catches the error, prints its type using type(e) and displays the error message using e.
🌐
Python.org
discuss.python.org › ideas
Print exception notes - in repr(exc) or otherwise - Ideas - Discussions on Python.org
September 4, 2024 - Exception notes are printed in tracebacks, but they are omitted when an exception is handled and logged or printed. I think a possibly helpful information is lost. # Python 3.11+ enote = ValueError("error message") enote.add_note("additional info") try: raise enote except Exception as exc: print(repr(exc)) # will NOT print 'additional info' Unless I overlooked something, the PEP 678 does not discuss the string representation, just the tracebacks.
🌐
Reddit
reddit.com › r/learnpython › how would i get my error message to print before python errors?
r/learnpython on Reddit: How would I get my error message to print before Python errors?
January 1, 2021 -

I am trying to make a pay calculator to calculate overtime, and my calculator is set up like this:

https://hastebin.com/wehuroruzi.py

From line 14 to 19 I state that If the 'pay_rate' input from the user isn't an integer or float point, then send an error message saying that the user must supply a number.

My issue is that Python gives me an error before the message can be sent to the user. The error being that whatever string that the user sent has no definition in the code.

Find elsewhere
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.7 documentation
Many standard modules define their own exceptions to report errors that may occur in functions they define. The try statement has another optional clause which is intended to define clean-up actions that must be executed under all circumstances. For example: >>> try: ... raise KeyboardInterrupt ... finally: ... print('Goodbye, world!') ...
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
March 18, 2026 - The code nested under the else clause, however, doesn’t execute, because Python encountered an exception during execution. Note that structuring your code like this is different from just adding the call to print() outside of the context of the try … except block: ... # ... try: linux_interaction() except RuntimeError as error: print(error) print("Doing even more Linux things.")
🌐
Coursera
coursera.org › tutorials › how to catch, raise, and print a python exception
How to Catch, Raise, and Print a Python Exception | Coursera
August 13, 2024 - The method above demonstrates how to catch all exceptions in Python. However, many different types of errors can arise from the code you put inside the try block. If you don’t specify which exceptions a particular except clause should catch, it will handle each one the same way. You can address this issue in a few ways. For example, you can anticipate the errors that may occur and add corresponding except blocks for each one. ... 1 2 3 4 5 6 7 8 9 10 11 12 a = 5 b = "zero" try: quotient = a / b print(quotient) except ZeroDivisionError: print("You cannot divide by zero") except TypeError: print("You must convert strings to floats or integers before dividing") except NameError: print("A variable you're trying to use does not exist")
🌐
Tagline Infotech
taglineinfotech.com › home › how do i print an exception in python?
How do I Print an Exception in Python? - Tagline Infotech
December 31, 2025 - When using Python try except print error to handle exceptions, the ability to print the exception stack trace provides insight into the python print error line of the code where the exception occurred, as well as the function calls that led up to the exception.
🌐
Python.org
discuss.python.org › python help
How to get the error line? - Python Help - Discussions on Python.org
February 9, 2024 - I want to ask in general when I execute small code from the terminal I get the error but I dont know which line cause this error so how I know which line ? Thanks
🌐
Reddit
reddit.com › r/pythontips › how can i use try except without hiding the stack trace?
r/pythontips on Reddit: How can I use Try Except without hiding the stack trace?
September 19, 2025 -

I've been dealing with legacy code that uses Try Excepts. The issue I'm having is that when a failure occurs the stack trace points to the line the Except is on (well the line below that's reporting it).

Code looks a little like this:

try:
  ...
except Exception as e:
  print(f"Error {str(repr(e))}")

Is this legacy code written incorrectly? Is there a reason we don't want to stack trace?

Maybe I'm wrong and this is returning the stacktrace but in a file that I'm not looking at, but I wanted to double check because so far Excepts seem to be a hidderance for me when I'm troubleshooting.

🌐
Bacancy Technology
bacancytechnology.com › qanda › python › python-try-except-print-error
How to print as exception in Python
July 31, 2023 - This exception object contains ... message associated with the exception. To print an exception in Python, you can use the print() function....
🌐
Rollbar
rollbar.com › home › what is “except exception as e” in python?
except Exception as e in Python: When and How to Use It
The exception is assigned to the variable e, which contains the error message "division by zero". The print(f"An error occurred: {e}") statement prints the error message to the console.
Published: August 17, 2026
🌐
Carmatec
carmatec.com › home › how to print and debug exceptions in python like a pro
How to Print and Debug Exceptions in Python Like a Pro
December 31, 2025 - Learn how to print and debug exceptions in Python like a pro with practical tips, code examples, and best practices for clean error handling.
🌐
DataCamp
datacamp.com › tutorial › exception-handling-python
Exception & Error Handling in Python | Tutorial by DataCamp | DataCamp
May 29, 2026 - Python 3.10 introduced structural pattern matching, which can be used to handle exceptions more elegantly by matching error types in a match statement. This is particularly useful when dealing with multiple exception types in a more readable way. Example: try: result = 1 / 0 except Exception as e: match e: case ZeroDivisionError(): print("You cannot divide by zero.") case NameError(): print("Variable not defined.") case _: print("An unexpected error occurred.")
🌐
W3Schools
w3schools.com › python › python_try_except.asp
Python Try Except
These exceptions can be handled using the try statement: The try block will generate an exception, because x is not defined: try: print(x) except: print("An exception occurred") Try it Yourself »
🌐
Finxter
blog.finxter.com › home › learn python blog › how to print exception messages in python (try-except)
How to Print Exception Messages in Python (Try-Except) - Be on the Right Side of Change
October 30, 2023 - The name of the exception type is printed. ... Displaying the exception type in Python is achieved by capturing the exception and utilizing type(e) to retrieve and print the type of exception that occurred.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exception-handling
Python Exception Handling - GeeksforGeeks
try: n = 0 res = 100 / n except ZeroDivisionError: print("You can't divide by zero!") except ValueError: print("Enter a valid number!") else: print("Result is", res) finally: print("Execution complete.") ... You can't divide by zero! Execution complete. Explanation: try block attempts division, except blocks catch specific errors, else block executes only if no errors occur, while finally block always runs, signaling end of execution. Refer to Python Built-in Exceptions for some common exceptions.
Published: May 29, 2026