print('start')
try:
number1 = 200
number2 = 0
answer = number1 / number2
print(answer)
except ZeroDivisionError as e:
print(e)
finally:
print('end')
実行結果
start
division by zero
end
tryブロック内で例外が発生すると、
変数eに例外をあらわすオブジェクトが代入される。
tryブロック内で例外が発生した場合、
そこで処理を中断し、
exceptブロックのコードを実行する。
例外処理の後に、
必ず実行したいコードは、
finallyブロックに記述する。