except errors python code example
Example 1: catch error python
import traceback
dict = {'a':3,'b':5,'c':8}
try:
print(dict[q])
except:
traceback.print_exc()
Example 2: python catch exception
try:
print('Test')
except (SyntaxError, IndexError) as E:
print('Synthax or index error !')
except :
print('Other error !')
else:
print('No error')
finally:
print('Done')
print('Anything else')
Example 3: python 3 except sys error info
1 import sys
2 try:
3 untrusted.execute()
4 except:
5 e = sys.exc_info()[0]
6 write_to_page( "<p>Error: %s</p>" % e )