開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のVII部(例外)のまとめ演習3(エラー処理)を解いてみる。
3.
python_program.py
コード(TextWrangler)
#!/usr/bin/env python #encoding: utf-8 class MyError: def __init__(self,data): self.data = data def opps1(): raise IndexError def opps2(): raise MyError('hello')
tools.py
コード(TextWrangler)
#!/usr/bin/env python #encoding: utf-8 def safe(func, *args): try: apply(func, *args) except: import sys, traceback print "type: ",sys.exc_type, "value: ",sys.exc_value print "traceback:" traceback.print_exc() from python_program import *
入出力結果(Terminal)
$ python Python 2.7.2 (default, Feb 12 2012, 23:50:38) [GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from tools import * >>> safe(opps1) type: <type 'exceptions.IndexError'> value: traceback: Traceback (most recent call last): File "tools.py", line 6, in safe apply(func, *args) File "python_program.py", line 9, in opps1 raise IndexError IndexError >>> safe(opps2) type: python_program.MyError value: <python_program.MyError instance at 0x1075a3830> traceback: Traceback (most recent call last): File "tools.py", line 6, in safe apply(func, *args) File "python_program.py", line 12, in opps2 raise MyError('hello') MyError: <python_program.MyError instance at 0x1075a3830> >>> quit() $
本書終了!
今日からPythonプログラマと名乗る資格を得た、履歴書に書き加えるべきらしい。(だけど自信無し。。)
0 コメント:
コメントを投稿