Errors
Handling Errors
try:
empty_list = []
matching_item = empty_list[0] # triggers an IndexError (list index out of range)
print("EVERYTHING IS GOING FINE") # this never gets reached
except IndexError:
print("OOPS - MY ERROR")
#> OOPS - MY ERRORtry:
100 / 0 # triggers a DivisionByZeroError
print("EVERYTHING IS GOING FINE") # this never gets reached
except DivisionByZeroError:
print("OOPS - MY ERROR")
#> OOPS - MY ERRORRaising Errors
Defining and Raising Custom Errors
Last updated