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 - For example, if you have a large ... 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

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
python - How do you print an error with Try and Except - Stack Overflow
I feel stupid asking this since the answer is probably simple but I couldn't really find the answer by googling it... I have a lot of code which I don't want help with, I want to debug on my own, a... More on stackoverflow.com
🌐 stackoverflow.com
Where to use try/except?
Use a try except block when: You have some part of your code that you expect could raise an exception, and When that exception occurs, you want something to happen other than the default behavior of immediately ending execution and displaying a traceback. More on reddit.com
🌐 r/learnpython
7
8
July 24, 2022
Why do we use try/except?

so what is the different here?

If you don't catch it, the program halts. If you use a try / except block, then you can program in what to do if the input is invalid. A normal choice would be to prompt the user that the input was invalid and try again.

Furthermore, it seems like all the try/except examples could be replicated with an if statements.

Yes at this point. But that won't always be the case. For example, you can't have an if statement that will tell if a http connection will work. Also, an if / else block does not gain you anything; it's just as long or longer as a try / except block, and slightly slower.

More important however is python's "duck typing" philosophy. In python, we don't waste time with tests; we blindly forge ahead and have the except block as a backup plan. In other words, if you expect a duck, and the object you get quacks, that's good enough. No need to test if it actually is a duck.

See python's definition's of duck-typing, EAFP and LBYL.

More on reddit.com
🌐 r/learnpython
13
33
August 17, 2017
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
🌐
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 »
🌐
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!') ...
🌐
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")
🌐
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:
Find elsewhere
🌐
Real Python
realpython.com › python-exceptions
Python Exceptions: An Introduction – Real Python
March 18, 2026 - What you didn’t get to see was the type of error that Python raised as a result of the function call. In order to see exactly what went wrong, you’d need to catch the error that the function raised. The following code is an example where you capture the RuntimeError and output that message to your screen: ... # ... try: linux_interaction() except RuntimeError as error: print(error) print("The linux_interaction() function wasn't executed.")
🌐
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 ...
🌐
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...
🌐
Programiz
programiz.com › python-programming › exception-handling
Python Exception Handling (With Examples)
try: numerator = 10 denominator = 0 result = numerator/denominator print(result) except: print("Error: Denominator cannot be 0.") # Output: Error: Denominator cannot be 0.
🌐
IQCode
iqcode.com › code › python › how-to-print-error-in-try-except-python
how to print error in try except python Code Example
November 10, 2021 - try: # some code except Exception as e: print("ERROR : "+str(e))
🌐
Bomberbot
bomberbot.com › python › python-print-exception-how-to-try-except-print-an-error
Python Print Exception – How to Try-Except-Print an Error - Bomberbot
January 8, 2025 - In addition to try and except, Python‘s exception handling syntax includes two other optional clauses: else and finally. The code in the else clause runs if the try clause does not raise an exception. This can be useful for code that should only run if no exceptions occurred: try: result = perform_complex_operation() except Exception: print("The operation failed.") else: print("The operation succeeded.") process_result(result)
🌐
GeeksforGeeks
geeksforgeeks.org › python-try-except
Python Try Except - GeeksforGeeks
March 19, 2025 - These blocks let you handle the errors without crashing the program. ... try: # Code that may raise an exception x = 3 / 0 print(x) except: # exception occurs, if code under try throws error print("An exception occurred.")
🌐
Python Tutorial
pythontutorial.net › home › python basics › python try…except
Python Try Except: How to Handle Exceptions More Gracefully
March 30, 2025 - Please enter a number for net sales.') except ZeroDivisionError: print('Error! The prior net sales cannot be zero.') Code language: Python (python) When you enter zero for the net sales of the prior period: Enter the net sales for - Prior period:0 - Current period:120Code language: Shell Session (shell) ... It’s a good practice to catch other general errors by placing the catch Exception block at the end of the list: try: # get input net sales print('Enter the net sales for') previous = float(input('- Prior period:')) current = float(input('- Current period:')) # calculate the change in percentage change = (current - previous) * 100 / previous # show the result if change > 0: result = f'Sales increase {abs(change)}%' else: result = f'Sales decrease {abs(change)}%' print(result) except ValueError: print('Error!
🌐
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)
🌐
Position Is Everything
positioniseverything.net › home › python try except print error: a guide to error handling
Python Try Except Print Error: A Guide to Error Handling - Position Is Everything
May 16, 2026 - This is the closest equivalent to “print the full error” after catching it. It is useful during development, command-line tools, background jobs, and logging. Avoid showing full tracebacks directly to ordinary users because they can be confusing and may reveal file paths or internal details. Python error handling has four related clauses: try, except, else, and finally.
🌐
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 Basics
pythonbasics.org › home › python basics › try and except in python
Try and Except in Python - pythonbasics.org
$ python3 example.py Divided by zero Should reach here · In the above example we catch the specific exception ZeroDivisionError. You can handle any exception like this: ... try: # your code here except FileNotFoundError: # handle exception except IsADirectoryError: # handle exception except: # all other types of exceptions print('Should reach here')