print('start')
try:
number1 = 200
number2 = 0
answer = number1 / number2
print(answer)
except ZeroDivisionError as e:
print(e)
finally:
print('end')
Execution result
start
division by zero
end
When an exception occurs in the try block, The object representing the exception is assigned to the variable e.
If an exception occurs inside the try block Then interrupt the process Execute the code of the except block.
After exception handling The code you definitely want to execute is Describe in the finally block.
Recommended Posts