Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k 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 (106k 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 Mar 3, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...