Misael
0
Q:

python catch any exception

import traceback

dict = {'a':3,'b':5,'c':8}
try:
  print(dict[q])
 
except:
  traceback.print_exc()
  
# This will trace you back to the line where everything went wrong. 
# So in this case you will get back line 5    
  
  
2
   1 import sys
   2 try:
   3   untrusted.execute()
   4 except: # catch *all* exceptions
   5   e = sys.exc_info()[0]
   6   write_to_page( "<p>Error: %s</p>" % e )
4
try:
    do_something()
except:
    print "Caught it!"
0
>>> 10 * (1/0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> 4 + spam*3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'spam' is not defined
>>> '2' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
-3

New to Communities?

Join the community