catch exception in python code example
Example 1: exception pyton print
except Exception as e: print(e)
Example 2: python try catch
try:
except ValueError:
except (BadThingError, HorrbileThingError) as e:
except:
else:
finally:
Example 3: except as Exception:
>>> def catch():
... try:
... asd()
... except Exception as e:
... print e.message, e.args
...
>>> catch()
global name 'asd' is not defined ("global name 'asd' is not defined",)
Example 4: catch error python
import traceback
dict = {'a':3,'b':5,'c':8}
try:
print(dict[q])
except:
traceback.print_exc()
Example 5: 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 6: python catch any exception
try:
do_something()
except:
print "Caught it!"