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 OverflowFor 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)
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()
Output:
Traceback (most recent call last):
File "C:\scripts\divide_by_zero.py", line 4, in <module>
1/0
ZeroDivisionError: division by zero
What’s the correct way to print an exception in Python when handling errors? - Ask a Question - TestMu AI Community
How to get the error line?
Try/Except isn't printing my message
Print exception notes - in repr(exc) or otherwise - Ideas - Discussions on Python.org
What does it mean by "print an exception" in Python?
Why is printing exceptions crucial?
Videos
Some reason I cant get my except print statement to print to terminal. It just keeps asking my original question unless the input matches.
def main(): making_faces()
def making_faces(): while True: try: user_input = input("':)' or ':(' ? ") if user_input == ":)": print("Hello! 😀") break elif user_input == ":(": print("Goodbye. 😟") break except: print("Please enter a valid Emoji")
main()
What the hell is up with Reddit Code formatting. It completely ignores it as code.