0
1

More than 3 years have passed since last update.

pythonのtry~except~else

Posted at

コード


def func(a, b):                                                                                   
    try:
        print(a / b)
    except ZeroDivisionError as e:
        print('例外Error:', e)
    else:
        print('finish')

# 正常
func(1, 2)

# 0で割れないので例外エラー
func(1, 0)

実行結果

0.5
finish
例外Error: division by zero
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