try...except...else...finally

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(Omi Chiba)

try...except...else...finally

Pythonは、割り込みを投げる、例外を起こす、ことができます。

Python can throw - pardon, raise - Exceptions:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

>>> try:

>>> a = 1 / 0

>>> except Exception, e

>>> print 'oops: %s' % e

>>> else:

>>> print 'no problem here'

>>> finally:

>>> print 'done'

oops: integer division or modulo by zero

done

例外が発生したときは、except句で補足され、その句が実行されますが、else句は実行されません。例外が発生しなかった場合は、except句は実行されず、else句が実行されます。finally句は常に実行されます。

If the exception is raised, it is caught by the except clause, which is executed, while the elseclause is not. If no exception is raised, the except clause is not executed, but the else one is. The finally clause is always executed.

異なる可能な例外のために複数のexcept句をとることができます:

There can be multiple except clauses for different possible exceptions:

1.

2.

3.

4.

5.

6.

7.

>>> try:

>>> raise SyntaxError

>>> except ValueError:

>>> print 'value error'

>>> except SyntaxError:

>>> print 'syntax error'

syntax error

elseとfinallyは省略可能です。

The else and finally clauses are optional.

以下は、組み込みのPython例外のリストと(web2pyで定義されている)HTTP例外です。

Here is a list of built-in Python exceptions + HTTP (defined by web2py)

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31.

32.

33.

34.

35.

36.

37.

38.

39.

40.

41.

42.

43.

44.

45.

46.

47.

48.

49.

BaseException

+-- HTTP (defined by web2py)

+-- SystemExit

+-- KeyboardInterrupt

+-- Exception

+-- GeneratorExit

+-- StopIteration

+-- StandardError

| +-- ArithmeticError

| | +-- FloatingPointError

| | +-- OverflowError

| | +-- ZeroDivisionError

| +-- AssertionError

| +-- AttributeError

| +-- EnvironmentError

| | +-- IOError

| | +-- OSError

| | +-- WindowsError (Windows)

| | +-- VMSError (VMS)

| +-- EOFError

| +-- ImportError

| +-- LookupError

| | +-- IndexError

| | +-- KeyError

| +-- MemoryError

| +-- NameError

| | +-- UnboundLocalError

| +-- ReferenceError

| +-- RuntimeError

| | +-- NotImplementedError

| +-- SyntaxError

| | +-- IndentationError

| | +-- TabError

| +-- SystemError

| +-- TypeError

| +-- ValueError

| | +-- UnicodeError

| | +-- UnicodeDecodeError

| | +-- UnicodeEncodeError

| | +-- UnicodeTranslateError

+-- Warning

+-- DeprecationWarning

+-- PendingDeprecationWarning

+-- RuntimeWarning

+-- SyntaxWarning

+-- UserWarning

+-- FutureWarning

+-- ImportWarning

+-- UnicodeWarning

各項目の詳細については、公式のPythonドキュメントを参照してください。

For a detailed description of each of them, refer to the official Python documentation.

web2pyは、HTTPと呼ばれる新しい例外を1つだけ公開しています。その例外が発生すると、プログラムはHTTPエラーページを返すようになります(詳細は第4章を参照してください)。

web2py exposes only one new exception, called HTTP. When raised, it causes the program to return an HTTP error page (for more on this refer to Chapter 4).

任意のオブジェクトは例外として発生させることができますが、それには組み込みのexceptionクラスを拡張したオブジェクトの利用を推奨します。

Any object can be raised as an exception, but it is good practice to raise objects that extend one of the built-in exception classes.