1try:
2 someFunction()
3except Exception as ex:
4 template = "An exception of type {0} occurred. Arguments:\n{1!r}"
5 message = template.format(type(ex).__name__, ex.args)
6 print (message)
1# Raise is used to cause an error
2raise(Exception("Put whatever you want here!"))
3raise(TypeError)
1try:
2 print("I will try to print this line of code")
3except:
4 print("I will print this line of code if an error is encountered")
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.
1try:
2 print("try to run this block")
3except:
4 print("run this bock if there was error in earlier block")