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# Python try: except:
2
3try:
4 print(a + b) # Program will try the add b to a
5except:
6 print("There was an error") # If the program will have an error in the try block
7 # The except block will run
8 # except block will run and then the program will continue to run
9
10# Examples:
11a = 1
12b = 1 # <===== no error, except block skipped
13
14a = 1
15b = 'one' # <===== error, except block run