| English | 意味 |
|---|---|
| exception/ekˈsepʃn/ | 例外 |
| exception handling/ekˈsepʃn ˈhændlɪŋ/ | 例外処理 |
| raise/reɪz/ | 発生 |
The check that cannot be written
- A program opens a file. Before opening it, you could check that the file exists, that it is readable, that the disk is present. Suppose every check passes.
- Between the last check and the open, a user pulls out the USB stick. The file existed when you asked and does not exist when you open it, and no amount of checking in advance could have closed that gap.
- Some errors are simply not preventable, because the world changes between the test and the action. What a program can do is respond to them when they happen.
- This lesson is the exception 异常, the TRY structure that handles one, and where in a program an exception should be caught.
What an exception is and why handle it
- An exception is an error that occurs during execution: division by zero, a file not found, invalid input, a network that has gone away.
- Exception handling 异常处理 lets the program detect the error and respond in a controlled way, instead of crashing and losing the user's work.
- Real programs face errors that cannot be prevented in advance, as the hook shows. And without exceptions, every single operation would need its own
IFcheck around it, burying the actual algorithm in error tests. - The third benefit is the one the exam most often asks for: it separates the normal flow from the error handling, so the main path reads cleanly.
例外とは:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
例外処理は、実行時の問題(ゼロでの除算、ファイルが見つからないなど)を、適切に対応できる仕組みです。
なぜすべてのエラーを事前にチェックするのではなく、例外処理を使うのか?適用されるものをすべて選択してください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
メリットは正確さと明瞭さです。速度向上ではありません;処理には若干のオーバーヘッドがかかるため、そのコストは正当な投資です。
The TRY structure
TRY
OPENFILE "data.txt" FOR READ
READFILE "data.txt", line
CLOSEFILE "data.txt"
EXCEPT FileNotFound
OUTPUT "Sorry, the file does not exist."
EXCEPT ReadError
OUTPUT "Sorry, there was an error reading the file."
FINALLY
OUTPUT "Finished attempting to read."
ENDTRY
- The TRY block holds the code that might fail. If it runs without error, the EXCEPT blocks are skipped entirely.
- If an exception occurs, execution jumps immediately to the first matching EXCEPT block. The rest of the TRY block does not run.
- A FINALLY block runs whether or not an exception happened, which makes it the right place for cleanup such as closing a file.

One path when all is well, another when it is not, and one that always runs
例外処理の流れ
コードが失敗した際に行われることを順を追って確認する。例外は通常のフローからジャンプしてハンドラへ移り、FINALLYはどちらの場合でも清掃を行い、プログラムは crash せず継続する。
各例外処理キーワードとその役割を対応させてください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
TRYは危険なコードを保護し、EXCEPTは例外を捕獲し、FINALLYはどちらの場合でもクリーンアップを行い、RAISEは捕獲されるべきエラーをスローします。
FINALLYブロックについて:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
FINALLYは必ず実行されるため、ファイル閉じなどのクリーンアップに最適です。
例外が発生したか否かに関わらず実行され、ファイル閉じに適しているブロックは ____ です。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
クリーンアップは両方のパスで必要です。CLOSEFILEをTRYブロックにのみ配置すると、エラーによってファイルが開いたままになった瞬間にスキップされてしまいます。
Worked example: trace the flow
- In the code above, the file does not exist. State exactly what is output and what is skipped.
OPENFILEraises FileNotFound, so execution leaves the TRY block immediately: theREADFILEandCLOSEFILElines never run.- The
FileNotFoundEXCEPT block runs, outputting "Sorry, the file does not exist." TheReadErrorblock does not run, because only the first matching handler is used. - The FINALLY block runs, outputting "Finished attempting to read."
- The mark most often lost is noticing that the rest of the TRY block is abandoned.
TRYブロック内のファイルが存在しない場合、結果の順序を並べ替えてください。
難易度 3/5 · 200 コイン + 今日初回完了時に小ボーナス付与
一致するハンドラは最初のみ実行され、FINALLYは必ず実行されます。スキップされた残りのTRYブロックが最も見落としやすいポイントです。
Raising an exception
- A subroutine that detects a problem it cannot sensibly deal with can raise 抛出 an exception, passing responsibility to whoever called it:
IF b = 0 THEN RAISE DivideByZero
- This is the right design when the subroutine knows something is wrong but not what should be done about it. A division routine knows the divisor is zero; only the caller knows whether to ask the user again, use a default, or abandon the calculation.
サブルーチンはRAISEを使って:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
RAISEは例外を呼び出し主にスローし、EXCEPTで捕獲できます。
なぜ割り算サブルーチンはゼロ除算を自身で処理せず、例外をスローするのか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
ユーザーに再入力を求める、デフォルト値を使用する、計算を放棄する:これらの選択肢は、コンテキストを持つ呼び出し側コードのみが行えます。
Where to handle it
- Handle an exception close to where it occurs when the response is simple and local: print a message, use a default, ask the user to try again.
- Handle it higher up when the decision belongs to a larger part of the program: whether to abandon a whole transaction, roll back a change, or tell the user the operation failed.
- The rule of thumb: catch it at the level that has enough information to decide what to do, not at the level that first notices.
Worked example: what not to do
- A student writes
EXCEPT: (do nothing)around a whole program so it never crashes. Explain why this is poor practice. - The exception is swallowed: the program continues as though nothing went wrong, so it carries on with missing or invalid data and produces wrong results instead of an obvious failure.
- A silent failure is harder to diagnose than a crash, because there is no message and no indication of where it happened.
- Catching every exception in one place also means the specific error type is lost, so no sensible response can be chosen. A handler should catch a specific exception and actually respond to it.
例外を静かに「吞み込む」(キャッチして何も行わない)ことは、実際のエラーを隠蔽し、デバッグを困難にします——処理するか、少なくともログに残すべきです。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
空のハンドラは、発見すべき問題そのものを隠してしまいます;常に反応するか、エラーを記録してください。
Marks that slip away
- When an exception occurs, the rest of the TRY block is skipped. Say so when tracing.
- Only the first matching EXCEPT runs, not all of them.
- FINALLY always runs, error or no error, which is what makes it right for closing files.
- Do not catch an exception and do nothing. A swallowed error is worse than a crash, because the program continues with bad data.
You've got it
- an exception is a run-time error; handling it lets a program respond instead of crashing, covers errors that cannot be prevented in advance, and separates normal code from error handling
- TRY holds the risky code; on an error the rest of it is skipped and the first matching EXCEPT runs; FINALLY runs either way, so cleanup belongs there
- a subroutine that cannot decide the response should RAISE the exception for its caller
- handle it close by for a simple local response, higher up when the decision needs a wider view; never catch it and do nothing