Try except for exception handling
try, except, else, finally
The try except block
try:
file = open('exists.txt')
print('File exists')
except Exception:
print('File does not exist')File existstry:
file = open('missing.txt')
print('File exists')
except Exception:
print('File does not exist')File does not existSpecifying the exception type
try:
file = open('exists.txt')
print('File exists')
except FileNotFoundError:
print('File does not exist')
except Exception:
print('An error occurred')Catching specific exception information
Using else in a try except block
Using finally in a try except block
Last updated