開発環境
- 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、1.(try/except)を解いてみる。
その他参考書籍
1.(try/except)
コード(BBEdit)
sample.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
def oops():
raise IndexError()
def func():
try:
oops()
except IndexError:
print('func:index error')
def opps1():
raise KeyError()
def func1():
try:
oops1()
except IndexError:
print('func1: index error')
except:
print('func1: error')
if __name__ == '__main__':
func() # IndexError
func1() # IndexErrorでKeyErrorは捕捉できない
入出力結果(Terminal)
$ ./sample.py func:index error func1: error $
0 コメント:
コメントを投稿