1try:
2 # Dangerous stuff
3except ValueError:
4 # If you use try, at least 1 except block is mandatory!
5 # Handle it somehow / ignore
6except (BadThingError, HorrbileThingError) as e:
7 # Hande it differently
8except:
9 # This will catch every exception.
10else:
11 # Else block is not mandatory.
12 # Dangerous stuff ended with no exception
13finally:
14 # Finally block is not mandatory.
15 # This will ALWAYS happen after the above blocks.
1>>> def catch():
2... try:
3... asd()
4... except Exception as e:
5... print e.message, e.args
6...
7>>> catch()
8global name 'asd' is not defined ("global name 'asd' is not defined",)
1try:
2 print("I will try to print this line of code")
3except ERROR_NAME:
4 print("I will print this line of code if error ERROR_NAME is encountered")
1 1 (x,y) = (5,0)
2 2 try:
3 3 z = x/y
4 4 except ZeroDivisionError as e:
5 5 z = e # representation: "<exceptions.ZeroDivisionError instance at 0x817426c>"
6 6 print z # output: "integer division or modulo by zero"