16
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonの例外処理「except」は複数連結出来る

Last updated at Posted at 2018-01-06

小ネタ。
常識っちゃ常識らしいがしらなかったので。

環境

Python 3.6.2

example.py
def example():
    try:
        num = float(input("入力する値は?"))
    except KeyboardInterrupt:
        print ("終了します.")
        sys.exit()
    except ValueError:
        print ("値が不正です")
        example()
    except Exception as e :
        print ("不明なエラーです")
        print (e)
        sys.exit(1)
   

というようにエラーの内容別に処理を分けることが出来る。

16
11
2

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
16
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?