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
🌐
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 »
Discussions

What’s the correct way to print an exception in Python when handling errors? - Ask a Question - TestMu AI Community
I’m a bit confused about the proper way to display errors when using a try/except block in Python. I’m catching exceptions successfully, but I’m not sure how to actually print the exception in a meaningful way. Right now, my except block runs, but I don’t know how to access or display ... More on community.testmuai.com
🌐 community.testmuai.com
0
December 22, 2025
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
0
0
February 9, 2024
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
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
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
🌐
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
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.3 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!') ...
🌐
Server Academy
serveracademy.com › blog › python-try-except
Python Try Except - Server Academy
In Python, try and except are used to catch and handle exceptions, allowing your program to continue executing or provide custom error messages rather than crashing. Here’s a basic syntax for using try and except: try: # Code that might raise an exception except SomeException: # Code to run if the exception occurs · Let’s look at a simple example to handle division by zero: try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!")
Find elsewhere
🌐
TestMu AI Community
community.testmuai.com › ask a question
What’s the correct way to print an exception in Python when handling errors? - Ask a Question - TestMu AI Community
December 22, 2025 - I’m a bit confused about the proper way to display errors when using a try/except block in Python. I’m catching exceptions successfully, but I’m not sure how to actually print the exception in a meaningful way. Right now, my except block runs, but I don’t know how to access or display ...
🌐
Bacancy Technology
bacancytechnology.com › qanda › python › python-try-except-print-error
How to print as exception in Python
July 31, 2023 - To print an exception in Python, you can use the print() function. The print() function can be used to print any object, including exception objects. For example, the following code will print an exception that occurs when we try to divide by zero:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-print-exception
Python Print Exception - GeeksforGeeks
July 23, 2025 - We catch a specific error like ValueError or ZeroDivisionError and print the message directly. This is the most common and beginner-friendly way to print exceptions. It keeps the output clean and easy to understand. ... try: num = int(input("Enter a number: ")) print(10 / num) except ValueError as ve: print("ValueError:", ve) except ZeroDivisionError as zde: print("ZeroDivisionError:", zde)
🌐
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 ... 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...
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
December 1, 2024 - Nesting code under the else clause assures that it’ll only run when Python doesn’t encounter any exception when executing the try … except block. You can also create a nested try … except block inside the else clause and catch possible exceptions there as well: ... # ... try: linux_interaction() except RuntimeError as error: print(error) else: try: with open("file.log") as file: read_data = file.read() except FileNotFoundError as fnf_error: print(fnf_error)
🌐
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
🌐
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 - To catch and print an exception that occurred in a code snippet, wrap it in an indented try block, followed by the command "except Exception as e" that catches the exception and saves its error message in string variable e. You can now print ...
🌐
Coursera
coursera.org › tutorials › python-exception
How to Catch, Raise, and Print a Python Exception | Coursera
August 13, 2024 - Add an except statement to this code that prints "You can only concatenate strings to strings" if there's a TypeError and "Something else went wrong" for any other type of error. ... 1 2 3 4 5 6 7 try: greeting = word1+word2 print(greeting) ...
🌐
Rollbar
rollbar.com › home › what is “except exception as e” in python?
What is “except Exception as e” in Python? | Rollbar
June 24, 2024 - 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.
🌐
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 - python try: result = 10 / 0 except ZeroDivisionError as e: print(f"An error occurred: {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.
🌐
DataCamp
datacamp.com › tutorial › python-try-except
Python Try-Except Tutorial: Best Practices and Real-World Examples | DataCamp
August 26, 2025 - try: result = some_function() except (TypeError, ValueError): print("Something was wrong with the data.") This way, you’re still clear about what might go wrong, without duplicating the same block over and over. And then there’s except Exception, which is better than a bare except, but ...
🌐
Metana
metana.io › metana: coding bootcamp | software, web3 & cyber › metana: coding bootcamp | software, web3 & cyber › full-stack › programming › mastering python exception handling: try-except best practices & examples (2025) › mastering python exception handling: try-except best practices & examples (2025)
Mastering Python Exception Handling: Try-Except Best Practices & Examples (2025)
December 1, 2025 - Python’s try-except block helps handle exceptions gracefully, preventing crashes. Always catch specific exceptions instead of using generic except Exception. Use else for code that runs only if no exceptions occur, improving readability. Utilize finally for necessary cleanup, such as closing files or releasing resources. Keep try blocks small and scoped to improve error isolation and debugging.