What is an exception? Usually it's an error, an indication that something went wrong. (Not all exceptions are errors, but never mind that for now.) Some programming languages encourage the use of error return codes, which you check. Python encourages the use of exceptions, which you handle.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
例外と見なされる
be regarded as an aberration
例外なき構造改革を求める
call for structural reforms without exceptions
例外なく
across the board
every time〈話〉
to a man
without (any) [with no] exception
without (any) [with no] known exception
【副】
definitely
flatly
unexceptionally
universally
例外なく当てはまる
apply universally
例外なく接近していく。
Without exception they are drawing it closer.〔【出典】Hiragana Times, 1993年8月号◆【出版社】株式会社ヤック企画 〕"HT082029", "2571477"
Documenting Exceptions Because PHP, unlike Java, does not require you to explicitly state which exceptions a method throws in the method signature, it is critical that exceptions be thoroughly documented in your method headers.〔【出典】PEAR ◆【License】Open Publication License ◆【編集】独立行政法人情報通信研究機構 〕
例外によって規則があることが分かる。
The exception proves the rule.〔古代ローマのキケロが提案したとされる考え方。例えば「日曜日は駐車禁止」という貼り紙(例外)があれば、その他の曜日は駐車して良いという規則があることが分かる。この場合のproveはconfirmの意味で使われている。しかし、この本来の考え方が誤解されて使われるようになり、「例外がその規則を適用できるかどうか検査する」や「規則には必ず例外があるものだ」という意味にもなることがある。〕
Exceptions are everywhere in Python. Virtually every module in the standard Python library uses them, and Python itself will raise them in a lot of different circumstances. You'll see them repeatedly throughout this book.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
You don't need to handle an exception in the function that raises it. If one function doesn't handle it, the exception is passed to the calling function, then that function's calling function, and so on "up the stack." If the exception is never handled, your program will crash, Python will print a "traceback" to standard error, and that's the end of that. Again, maybe that's what you want; it depends on what your program does.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
例外は、先の節で述べた定義によるエラーが発生した場合に常にスローしなければなりません。
An exception should be thrown whenever an error condition is met, according to the definition provided in the previous section.〔【出典】PEAR ◆【License】Open Publication License ◆【編集】独立行政法人情報通信研究機構 〕
An exception doesn't need to result in a complete program crash, though. Exceptions can be handled. Sometimes an exception is really because you have a bug in your code (like accessing a variable that doesn't exist), but sometimes an exception is something you can anticipate. If you're opening a file, it might not exist. If you're importing a module, it might not be installed. If you're connecting to a database, it might be unavailable, or you might not have the correct security credentials to access it. If you know a line of code may raise an exception, you should handle the exception using a try...except block.〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕
One exception is that in golf, which was brought over from abroad, there is a common practice of tipping the caddy.〔【出典】『日本人の法則』(長谷川勝行著)◆【出版社】株式会社ヤック企画 〕"YA21-139", "2423691"
I know there are exceptions, but I personally have always been treated kindly and respectfully by strangers as well as in public places such as hospitals, etc.〔【出典】Hiragana Times, 1996年10月号◆【出版社】株式会社ヤック企画 〕"HT120019", "2337225"
例外を発生させる構文は実にシンプルだ。raise文を使い、その後に例外名と、オプションでデバッグ用の人間が読める文字列を置く。この構文は関数呼び出しに似ている(実際に、例外はクラスとして実装されており、raiseはValueErrorクラスのインスタンスを生成し、'number must be non-negative'という文字列をインスタンスの初期化メソッドに渡す。しかしこの話は先走り過ぎだ!)。
The syntax for raising an exception is simple enough. Use the raise statement, followed by the exception name, and an optional human-readable string for debugging purposes. The syntax is reminiscent of calling a function. (In reality, exceptions are implemented as classes, and this raise statement is actually creating an instance of the ValueError class and passing the string 'number must be non-negative' to its initialization method. But we're getting ahead of ourselves!)〔【出典】"Dive Into Python 3" by Mark Pilgrim ◆【和訳】Fukada & Fujimoto ◆【License】CC-BY-SA-3.0 〕