B2.1.3 · Programming fundamentals
Exception handling
By the end you can
Trace try-catch/except and finally blocks, handling runtime exceptions gracefully without abnormal crashes.
In a perfect world, users never enter letters when numbers are expected, network connections never drop, and files always exist. In real-world software engineering, unexpected runtime conditions occur constantly. provides a structured way to detect, intercept, and recover from runtime anomalies without terminating the application abnormally.
The try, catch, and finally pattern#
Structured exception handling separates normal business logic from error-recovery routines using three primary blocks:
- The
tryblock: Code that might fail (such as parsing user input, reading from disk, or querying a network endpoint) is placed insidetry. If an error occurs, execution inside thetryblock halts immediately, and control transfers to the matching handler. - The
catch(orexceptin Python) block: Intercepts the exception object. Here, the programmer can log the issue, notify the user with an intelligible error message, or initiate a fallback strategy. - The
finallyblock: Always runs, whether an error was thrown or not. It is critical for releasing scarce system resources: such as closing open file descriptors, network sockets, or database connections.
Common runtime exceptions in the IB syllabus#
Students should recognise the root causes of standard exceptions in exam code traces:
- Division by zero: Attempting to evaluate
x / 0. Handled by checking divisor validity before calculation or catching the arithmetic exception. - Array index out of bounds: Attempting to access an index less than 0 or greater than or equal to array length.
- Null pointer / None access: Invoking a method or reading an attribute on an uninitialised object reference.
- Number format / Type mismatch: Calling integer parsing functions (e.g.
Integer.parseInt("hello")) on non-numeric input.
Check yourself
Exception handling
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 9 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Define the term exception handling and describe its purpose in robust software.
[2]Define: Give the precise meaning of the term.
2.Explain the role of the try, catch (or except), and finally blocks in structured error handling, and describe what happens if an unhandled exception occurs.
[4]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.State three common runtime exceptions that can occur during program execution.
[3]State: Give a short answer, no explanation needed.
In a nutshell
- Exception handling intercepts runtime anomalies to prevent abnormal program crashes and maintain data integrity.
- The try block encloses vulnerable code; the catch (except) block contains recovery logic; the finally block always executes to release resources.
- Common runtime errors include division by zero, array index out of bounds, null pointer access and number format parsing exceptions.
- Defensive input validation should prevent predictable errors before relying on exceptions.
