0 votes
1 view
in Python by (47.8k points)

Right now, I catch the exception in the except Exception: clause, and do print(exception). The result provides no information since it always prints <class'Exception'>. I knew this used to work in python 2, but how do I do it in python3?

2 Answers

0 votes
by (107k points)

To print an exception in Python 3 you need to assign the Exception to a variable. As shown in the Python 3 tutorial:

def fails():

x = 1 / 0 

try: 

fails() 

except Exception as ex: 

print(ex)

0 votes
by (140 points)

Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python:

>>> while True print('Hello world')
  File "<stdin>", line 1
    while True print('Hello world')
                   ^
SyntaxError: invalid syntax

Related questions

0 votes
2 answers
0 votes
1 answer
asked 4 days ago in Python by laddulakshana (9.5k points)
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...