How to print an error in Python?

+1 vote

try:
    something here
except:
    print 'the whatever error occurred.'

How can I print the error in my except: block?

Aug 23, 2018 in Python by bug_seeker
• 15,320 points
135 views

1 answer to this question.

0 votes

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)

answered Aug 23, 2018 by Priyaj
• 56,200 points

Related Questions In Python

0 votes
1 answer

How can I print an Error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Oct 12, 2018 in Python by ariaholic
• 7,320 points
31 views
+4 votes
7 answers
+2 votes
11 answers
0 votes
1 answer

How to print first character of each word in upper case of a string in Python

You can use split() using space as ...READ MORE

answered Jun 1, 2018 in Python by george
• 200 points
1,546 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 168 views
+4 votes
6 answers
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,710 points
1,498 views
+1 vote
1 answer

How to create plots using python matplotlib in IPython notebook?

I think you should try: I used %matplotlib inline in ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 56,200 points
115 views