エラーと例外
************

これまでエラーメッセージについては簡単に触れるだけでしたが、チュートリ
アル中の例を自分で試していたら、実際にいくつかのエラーメッセージを見て
いることでしょう。エラーには (少なくとも) 二つのはっきり異なる種類があ
ります。それは *構文エラー (syntax error)* と *例外 (exception)* です
。


構文エラー
==========

構文エラーは構文解析エラー (parsing error) としても知られており、
Python を勉強している間に最もよく遭遇する問題の一つでしょう:

   >>> while True print 'Hello world'
     File "<stdin>", line 1
       while True print 'Hello world'
                      ^
   SyntaxError: invalid syntax

The parser repeats the offending line and displays a little 『arrow』
pointing at the earliest point in the line where the error was
detected.  The error is caused by (or at least detected at) the token
*preceding* the arrow: in the example, the error is detected at the
keyword "print", since a colon ("':'") is missing before it.  File
name and line number are printed so you know where to look in case the
input came from a script.


例外
====

たとえ文や式が構文的に正しくても、実行しようとしたときにエラーが発生す
るかもしれません。実行中に検出されたエラーは *例外 (exception)* と呼ば
れ、常に致命的とは限りません。これから、Python プログラムで例外をどの
ように扱うかを学んでいきます。ほとんどの例外はプログラムで処理されず、
以下に示されるようなメッセージになります:

   >>> 10 * (1/0)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   ZeroDivisionError: integer division or modulo by zero
   >>> 4 + spam*3
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   NameError: name 'spam' is not defined
   >>> '2' + 2
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   TypeError: cannot concatenate 'str' and 'int' objects

エラーメッセージの最終行は何が起こったかを示しています。例外は様々な型
(type) で起こり、その型がエラーメッセージの一部として出力されます。上
の例での型は "ZeroDivisionError", "NameError", "TypeError" です。例外
型として出力される文字列は、発生した例外の組み込み名です。これは全ての
組み込み例外について成り立ちますが、ユーザ定義の例外では (成り立つよう
にするのは有意義な慣習ですが) 必ずしも成り立ちません。標準例外の名前は
組み込みの識別子です (予約語ではありません)。

残りの行は例外の詳細で、その例外の型と何が起きたかに依存します。

エラーメッセージの先頭部分では、例外が発生した実行コンテキスト
(context) を、スタックのトレースバック (stack traceback) の形式で示し
ています。一般には、この部分にはソースコード行をリストしたトレースバッ
クが表示されます。しかし、標準入力から読み取られたコードは表示されませ
ん。

組み込み例外 には、組み込み例外とその意味がリストされています。


例外を処理する
==============

例外を選別して処理するようなプログラムを書くことができます。以下の例を
見てください。この例では、有効な文字列が入力されるまでユーザに入力を促
しますが、ユーザがプログラムに ("Control-C" か、またはオペレーティング
システムがサポートしている何らかのキーを使って) 割り込みをかけてプログ
ラムを中断させることができるようにしています。ユーザが生成した割り込み
は、 "KeyboardInterrupt" 例外が送出されることで通知されるということに
注意してください。

   >>> while True:
   ...     try:
   ...         x = int(raw_input("Please enter a number: "))
   ...         break
   ...     except ValueError:
   ...         print "Oops!  That was no valid number.  Try again..."
   ...

"try" 文は下記のように動作します。

* まず、 *try 節 (try clause)* (キーワード "try" と "except" の間の
  文) が実行されます。

* 何も例外が発生しなければ、 *except 節* をスキップして "try" 文の実
  行 を終えます。

* try 節内の実行中に例外が発生すると、その節の残りは飛ばされます。次
  に 、例外型が "except" キーワードの後に指定されている例外に一致する
  場合 、except 節が実行された後、 "try" 文の後ろへ実行が継続されます
  。

* もしも except 節で指定された例外と一致しない例外が発生すると、その
  例 外は "try" 文の外側に渡されます。例外に対するハンドラ (handler、
  処理 部) がどこにもなければ、 *処理されない例外 (unhandled
  exception)* と なり、上記に示したようなメッセージを出して実行を停止
  します。

一つの "try" 文に複数の except 節を設けて、さまざまな例外に対するハン
ドラを指定することができます。同時に一つ以上のハンドラが実行されること
はありません。ハンドラは対応する try 節内で発生した例外だけを処理し、
同じ "try" 文内の別の例外ハンドラで起きた例外は処理しません。 except
節には複数の例外を丸括弧で囲ったタプルにして渡すことができます。例えば
以下のようにします:

   ... except (RuntimeError, TypeError, NameError):
   ...     pass

Note that the parentheses around this tuple are required, because
"except ValueError, e:" was the syntax used for what is normally
written as "except ValueError as e:" in modern Python (described
below). The old syntax is still supported for backwards compatibility.
This means "except RuntimeError, TypeError" is not equivalent to
"except (RuntimeError, TypeError):" but to "except RuntimeError as
TypeError:" which is not what you want.

最後の except 節では例外名を省いて、ワイルドカード (wildcard、総称記号
) にすることができます。ワイルドカードの except 節は非常に注意して使っ
てください。というのは、ワイルドカードは通常のプログラムエラーをたやす
く隠してしまうからです！ワイルドカードの except 節はエラーメッセージを
出力した後に例外を再送出する (関数やメソッドの呼び出し側が同様にして例
外を処理できるようにする) 用途にも使えます:

   import sys

   try:
       f = open('myfile.txt')
       s = f.readline()
       i = int(s.strip())
   except IOError as e:
       print "I/O error({0}): {1}".format(e.errno, e.strerror)
   except ValueError:
       print "Could not convert data to an integer."
   except:
       print "Unexpected error:", sys.exc_info()[0]
       raise

"try" … "except" 文には、オプションで *else 節 (else clause)* を設ける
ことができます。 "else" 節を設ける場合、全ての "except" 節よりも後ろに
置かなければなりません。 "else" 節は try 節で全く例外が送出されなかっ
たときに実行されるコードを書くのに役立ちます。例えば次のようにします:

   for arg in sys.argv[1:]:
       try:
           f = open(arg, 'r')
       except IOError:
           print 'cannot open', arg
       else:
           print arg, 'has', len(f.readlines()), 'lines'
           f.close()

追加のコードを追加するのは "try" 節の後ろよりも "else" 節の方がよいで
しょう。なぜなら、そうすることで "try" … "except" 文で保護したいコード
から送出されたもの以外の例外を偶然に捕捉してしまうという事態を避けられ
るからです。

例外が発生するとき、例外は関連付けられた値を持つことができます。この値
は例外の *引数 (argument)* とも呼ばれます。引数の有無および引数の型は
、例外の型に依存します。

The except clause may specify a variable after the exception name (or
tuple). The variable is bound to an exception instance with the
arguments stored in "instance.args".  For convenience, the exception
instance defines "__str__()" so the arguments can be printed directly
without having to reference ".args".

One may also instantiate an exception first before raising it and add
any attributes to it as desired.

   >>> try:
   ...     raise Exception('spam', 'eggs')
   ... except Exception as inst:
   ...     print type(inst)     # the exception instance
   ...     print inst.args      # arguments stored in .args
   ...     print inst           # __str__ allows args to be printed directly
   ...     x, y = inst.args
   ...     print 'x =', x
   ...     print 'y =', y
   ...
   <type 'exceptions.Exception'>
   ('spam', 'eggs')
   ('spam', 'eggs')
   x = spam
   y = eggs

If an exception has an argument, it is printed as the last part (『
detail』) of the message for unhandled exceptions.

例外ハンドラは、try 節の直接内側で発生した例外を処理するだけではなく、
その try 節から (たとえ間接的にでも) 呼び出された関数の内部で発生した
例外も処理します。例えば:

   >>> def this_fails():
   ...     x = 1/0
   ...
   >>> try:
   ...     this_fails()
   ... except ZeroDivisionError as detail:
   ...     print 'Handling run-time error:', detail
   ...
   Handling run-time error: integer division or modulo by zero


例外を送出する
==============

"raise" 文を使って、特定の例外を発生させることができます。例えば:

   >>> raise NameError('HiThere')
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   NameError: HiThere

The sole argument to "raise" indicates the exception to be raised.
This must be either an exception instance or an exception class (a
class that derives from "Exception").

例外が発生したかどうかを判定したいだけで、その例外を処理するつもりがな
ければ、単純な形式の "raise" 文を使って例外を再送出させることができま
す:

   >>> try:
   ...     raise NameError('HiThere')
   ... except NameError:
   ...     print 'An exception flew by!'
   ...     raise
   ...
   An exception flew by!
   Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
   NameError: HiThere


ユーザー定義例外
================

Programs may name their own exceptions by creating a new exception
class (see クラス for more about Python classes).  Exceptions should
typically be derived from the "Exception" class, either directly or
indirectly.  For example:

   >>> class MyError(Exception):
   ...     def __init__(self, value):
   ...         self.value = value
   ...     def __str__(self):
   ...         return repr(self.value)
   ...
   >>> try:
   ...     raise MyError(2*2)
   ... except MyError as e:
   ...     print 'My exception occurred, value:', e.value
   ...
   My exception occurred, value: 4
   >>> raise MyError('oops!')
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   __main__.MyError: 'oops!'

In this example, the default "__init__()" of "Exception" has been
overridden.  The new behavior simply creates the *value* attribute.
This replaces the default behavior of creating the *args* attribute.

例外クラスでは、普通のクラスができることなら何でも定義することができま
すが、通常は単純なものにしておきます。大抵は、いくつかの属性だけを提供
し、例外が発生したときにハンドラがエラーに関する情報を取り出せるように
する程度にとどめます。複数の別個の例外を送出するようなモジュールを作成
する際には、そのモジュールで定義されている例外の基底クラスを作成するの
が一般的なプラクティスです:

   class Error(Exception):
       """Base class for exceptions in this module."""
       pass

   class InputError(Error):
       """Exception raised for errors in the input.

       Attributes:
           expr -- input expression in which the error occurred
           msg  -- explanation of the error
       """

       def __init__(self, expr, msg):
           self.expr = expr
           self.msg = msg

   class TransitionError(Error):
       """Raised when an operation attempts a state transition that's not
       allowed.

       Attributes:
           prev -- state at beginning of transition
           next -- attempted new state
           msg  -- explanation of why the specific transition is not allowed
       """

       def __init__(self, prev, next, msg):
           self.prev = prev
           self.next = next
           self.msg = msg

ほとんどの例外は、標準の例外の名前付けと同様に、 「Error」 で終わる名
前で定義されています。

多くの標準モジュールでは、モジュールで定義されている関数内で発生する可
能性のあるエラーを報告させるために、独自の例外を定義しています。クラス
についての詳細な情報は クラス 章で提供されています。


クリーンアップ動作を定義する
============================

"try" 文にはもう一つオプションの節があります。この節はクリーンアップ動
作を定義するためのもので、どんな状況でも必ず実行されます。例を示します
:

   >>> try:
   ...     raise KeyboardInterrupt
   ... finally:
   ...     print 'Goodbye, world!'
   ...
   Goodbye, world!
   Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
   KeyboardInterrupt

A *finally clause* is always executed before leaving the "try"
statement, whether an exception has occurred or not. When an exception
has occurred in the "try" clause and has not been handled by an
"except" clause (or it has occurred in an "except" or "else" clause),
it is re-raised after the "finally" clause has been executed.  The
"finally" clause is also executed 「on the way out」 when any other
clause of the "try" statement is left via a "break", "continue" or
"return" statement.  A more complicated example (having "except" and
"finally" clauses in the same "try" statement works as of Python 2.5):

   >>> def divide(x, y):
   ...     try:
   ...         result = x / y
   ...     except ZeroDivisionError:
   ...         print "division by zero!"
   ...     else:
   ...         print "result is", result
   ...     finally:
   ...         print "executing finally clause"
   ...
   >>> divide(2, 1)
   result is 2
   executing finally clause
   >>> divide(2, 0)
   division by zero!
   executing finally clause
   >>> divide("2", "1")
   executing finally clause
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "<stdin>", line 3, in divide
   TypeError: unsupported operand type(s) for /: 'str' and 'str'

見てわかるとおり、 "finally" 節はどの場合にも実行されています。文字列
を割り算することで発生した "TypeError" は "except" 節で処理されていな
いので、 "finally" 節実行後に再度送出されています。

実世界のアプリケーションでは、 "finally" 節は(ファイルやネットワーク接
続などの)外部リソースを、利用が成功したかどうかにかかわらず解放するた
めに便利です。


定義済みクリーンアップ処理
==========================

オブジェクトのなかには、その利用の成否にかかわらず、不要になった際に実
行される標準的なクリーンアップ処理が定義されているものがあります。以下
の、ファイルをオープンして内容を画面に表示する例をみてください。

   for line in open("myfile.txt"):
       print line,

The problem with this code is that it leaves the file open for an
indeterminate amount of time after the code has finished executing.
This is not an issue in simple scripts, but can be a problem for
larger applications. The "with" statement allows objects like files to
be used in a way that ensures they are always cleaned up promptly and
correctly.

   with open("myfile.txt") as f:
       for line in f:
           print line,

After the statement is executed, the file *f* is always closed, even
if a problem was encountered while processing the lines. Other objects
which provide predefined clean-up actions will indicate this in their
documentation.
