開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART VII.(Exceptions and Tools)、Test Your Knowledge、2.(Exception objects and lists.)を解いてみる。
その他参考書籍
2.(Exception objects and lists.)
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- class MyError(Exception): pass def oops(): raise MyError('my error') def func(): try: oops() except (IndexError, MyError) as err: import sys print(sys.exc_info()[0:2]) print(type(err), err, err.args) def func1(): try: oops() except IndexError as err: print(err) except MyError as err: print(err) if __name__ == '__main__': func() func1()
入出力結果(Terminal)
$ ./sample.py (<class '__main__.MyError'>, MyError('my error',)) <class '__main__.MyError'> my error ('my error',) my error $
0 コメント:
コメントを投稿