Practice Flashcards
Flip to reveal answersWhat is an exception?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All 5 Flashcards — Exception handling
Sign up free to track progress and get spaced-repetition review schedules.
Question
What is an exception?
Answer
A problem that appears while the program is running, rather than a mistake in the code's grammar. Without handling the program simply stops; with handling it can report the problem and carry on.
Question
What do try, except and finally each do?
Answer
try holds only the lines that might fail. except (catch in Java) handles one specific kind of failure. finally runs whether or not anything failed, and is where files get closed.
Question
What are the three sources of failure to look for?
Answer
Unexpected input, usually from people; unavailable resources such as a missing file or unresponsive network; and logic errors, where the code ran on values nobody planned for.
Question
When should you validate rather than catch?
Answer
When the problem is predictable and inside your control — an empty list, a zero divisor. Catch the things outside it: files, networks and databases, which can fail between your check and your use.
Question
Why must closing a file go in finally?
Answer
If a line inside try fails, control jumps straight to except and any later lines in try never run. finally runs either way, so the file is closed rather than left open holding a system resource.
Read the notes
Full study notes for Exception handling
Topic 6.1 hub
Programming fundamentals
More from Topic 6.1
All flashcards in this topic
Computer Science exam skills
Paper structures & tips
Track your progress with spaced repetition
Sign up free — Aimnova tells you exactly which cards to review and when, so you remember everything before your IB exam.
Start Free