How to print an exception in Python

2 Answers

0 votes
s = 'python'
n = 12

try:
    n = n + s
    print(n)
except Exception as e: 
    print(repr(e))




'''
run:

TypeError("unsupported operand type(s) for +: 'int' and 'str'")

'''

 



answered Apr 27, 2021 by avibootz
0 votes
import traceback

s = 'python'
n = 12

try:
    n = n + s
    print(n)
except Exception as e: 
    traceback.print_exc()




'''
run:

Traceback (most recent call last):
  File "jdoodle.py", line 7, in <module>
    n = n + s
TypeError: unsupported operand type(s) for +: 'int' and 'str'

'''

 



answered Apr 27, 2021 by avibootz

Related questions

2 answers 275 views
3 answers 361 views
361 views asked Apr 27, 2021 by avibootz
4 answers 549 views
549 views asked Dec 11, 2020 by avibootz
1 answer 245 views
245 views asked Nov 25, 2018 by avibootz
1 answer 219 views
1 answer 289 views
1 answer 288 views
...