0
1

More than 3 years have passed since last update.

0で割ったエラー ZeroDivisionError の処理

Posted at
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ブロックに記述する。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1