Exceptions
Exceptions
try:
file = open('test.txt', 'rb')
except IOError as e:
print('An IOError occurred. {}'.format(e.args[-1]))Handling multiple exceptions
1. Putting all the exceptions in a tuple
try:
file = open('test.txt', 'rb')
except (IOError, EOFError) as e:
print("An error occurred. {}".format(e.args[-1]))2. Separate except blocks
except blocks3. Trapping ALL exceptions:
finally clause
finally clausetry/else clause
try/else clauseLast updated