Computer ScienceCore24 min read

Programming Paradigms and Exception Handling

Four ways of describing a computation, and what to do when it goes wrong

This topic appears in:

01

Four ways to describe the same computation

A paradigm is a style of organising a program — a view of what a program fundamentally is. The syllabus requires four, and the distinction that matters most is between saying how to do something and saying what you want.

Low-level programming works directly with the instruction set, giving complete control at the cost of readability. Imperative (procedural) programming gives an ordered sequence of statements that change state — the style of most introductory teaching. Object-oriented programming bundles data with the operations on it into objects. Declarative programming states the desired result and lets the system find it, which is what SQL does.

ParadigmProgram isExampleSuits
Low-levelmachine instructionsassemblydrivers, embedded control
Imperativea sequence of state changesC, Pythongeneral algorithms
Object-orientedobjects exchanging messagesJava, C++large systems, simulations
Declarativea statement of the resultSQL, Prologqueries, rule-based problems

The how-versus-what distinction

An imperative program to find tall students would loop through a list, test each height and append matches to a result. A declarative one says SELECT name FROM student WHERE height > 180 and never mentions looping at all. Both produce the same answer; only the second leaves the method to the system. That is the cleanest way to separate the paradigms in an answer.

02

The object-oriented ideas that get examined

Object-oriented programming is built on a small number of ideas, and the marks go to those who can explain the purpose of each rather than just define it.

Encapsulation keeps data and the methods that act on it together, with the data private so it can only be changed through methods that enforce the rules. Inheritance lets a subclass take on the members of a superclass, so shared behaviour is written once. Polymorphism lets the same method call behave differently depending on the object receiving it.

CLASS ShapePRIVATE name : STRINGPUBLIC PROCEDURE Area()// overridden by each subclassCLASS Circle INHERITS ShapePRIVATE radius : REALPUBLIC PROCEDURE Area()RETURN 3.14159 * radius * radiusCLASS Rectangle INHERITS ShapePUBLIC PROCEDURE Area()RETURN width * heightthe same call, Area(), runs different code depending on the object
PRIVATE
encapsulationreachable only through the class's own methods
INHERITS
inheritancethe subclass gains the superclass members
overriding
polymorphismthe same call resolves to different code

Say why, not just what

A definition of encapsulation earns one mark; the reason earns the second. Private data cannot be set to an invalid value by unrelated code, because every change must pass through a method that can check it — so the object is always in a valid state. The same applies to inheritance: the point is that shared code is written once, so a fix applies everywhere.

03

When things go wrong at run time

Some failures cannot be prevented by careful coding, because they depend on circumstances outside the program: a missing file, a full disk, a user typing letters into a number field, a division by a value that happened to be zero.

Exception handling separates the normal path from the error path. The risky code goes in a try block; if something fails, control jumps to a catch block that deals with it, and a finally block runs either way — which is where files get closed regardless of what happened.

The advantage over checking every operation with an if is that the normal logic stays readable, and an error can be handled at a sensible level rather than at the exact line where it occurred.

TRYopen the fileread and process the dataCATCH FileNotFoundreport the problem to the userCATCH InvalidFormatlog it and use a defaultFINALLYclose the fileENDTRYFINALLY runs whether or not an exception occurred
TRY
the risky codethe normal path, kept readable
CATCH
the handlerone per kind of exception you can deal with
FINALLY
cleanupruns either way — closing files, releasing resources

Good practice examiners look for

  1. Catch specific exceptions rather than everything indiscriminately.
  2. Never leave a catch block empty — a silently swallowed error is worse than a crash.
  3. Use finally to release resources, so files close even when something failed.
  4. Validate input first; exceptions are for the unpredictable, not for ordinary checking.
  5. Report something useful to the user rather than exposing a raw system message.
  6. File processing is the classic case, because the file may be missing, locked or corrupt.
04

Why a large system is usually object-oriented

The paradigms are not merely a matter of taste, and questions often ask which suits a described project. The deciding factor is usually how large the program is and how many people maintain it.

A procedural program of a few hundred lines is perfectly clear. At a hundred thousand lines the difficulty is no longer the algorithms but keeping track of which parts of the code can affect which pieces of data. Object orientation addresses exactly that: making attributes private means a bug that corrupts an account balance must be inside the account class, rather than anywhere in the program.

Inheritance and polymorphism then allow new cases to be added without touching working code. A new subclass of Shape needs its own Area method, and every existing routine that calls Area continues to work unchanged — which is why the paradigm scales.

  • Maintainability — a change to how data is stored affects only its own class.
  • Reusability — a well-written class can be used by another program unaltered.
  • Extensibility — new subclasses extend behaviour without editing existing code.
  • Team working — different people can own different classes, meeting only at the interfaces.
  • Modelling — objects map naturally onto real things, which suits simulations.

It is not free

Object orientation adds overhead — more code to write for a small task, a steeper learning curve, and a slight run-time cost for method calls and object creation. For a fifty-line script that reads a file and prints a total, a procedural program is genuinely the better engineering choice. A question asking you to justify a paradigm expects the trade-off, not an assertion that OOP is always superior.

Practice questions

5 questions · 15 marks · full working on every one

Try each one on paper first, then open the working. The marks are shown where they are actually awarded, because that is where they are actually lost.

Short questions

4 · 9 marks

Two marks each, in the style of the short-question section of the paper. Answer in two or three lines.

SQ1[2 marks]
State the difference between the imperative and declarative paradigms.
Model answer

An imperative program specifies how to obtain the result, as an ordered sequence of statements that change the program state. A declarative program specifies what result is required and leaves the method of obtaining it to the system — SQL being the standard example.

Examiner tip. The how-versus-what contrast is the mark; an example of each helps secure it.

SQ2[3 marks]
Explain what is meant by encapsulation and give one benefit of it.
Model answer

Encapsulation means bundling data together with the methods that operate on it inside a class, and making the data private so it can only be accessed through those methods. The benefit is that the data cannot be set to an invalid value by unrelated code, since every change passes through a method that can validate it — so the object stays in a consistent state.

Examiner tip. Three marks: the bundling, the private access, and a genuine benefit.

SQ3[2 marks]
Explain what polymorphism means, using an example.
Model answer

Polymorphism means the same method call behaves differently depending on the object it is applied to. For example, calling Area() on a Circle runs the circle's formula while the same call on a Rectangle runs a different one — the calling code does not need to know which type it holds.

Examiner tip. A concrete example is required; a bare definition rarely earns both marks.

SQ4[2 marks]
Give one situation where a low-level paradigm would be chosen over a high-level one, and explain why.
Model answer

Writing a device driver or code for an embedded microcontroller. Direct access to specific registers, memory addresses and hardware timing is required, and the precise number of clock cycles may matter — control a high-level language deliberately abstracts away.

Examiner tip. Both a plausible situation and the reason for needing that level of control are required.

Exam questions

1 · 6 marks

Multi-part questions with a full mark scheme.

Q1[6 marks]
(a) State what a try–catch–finally structure is for.
(b) Explain why the finally block is useful when working with files.
(c) Explain why catching all exceptions with a single generic handler is poor practice.
(d) Give one advantage of exception handling over checking every operation with an if statement.
Mark scheme
  1. (a) It separates the normal flow from error handling: risky code goes in TRY, and control passes to a CATCH block if an exception occurs.The separation of normal and error paths is the point.[1]
  2. (b) FINALLY runs whether or not an exception occurred, so the file is closed either way.The "either way" behaviour is what makes it useful.[1]
  3. Without it a file could be left open after an error, locking it or losing unwritten data.A concrete consequence earns the second mark.[1]
  4. (c) Different errors need different responses, and a generic handler treats them identically.Loss of specificity is the core issue.[1]
  5. It can also hide genuine bugs, since an unexpected error is caught and silently ignored rather than being noticed.The masking of programming errors is the stronger half of the answer.[1]
  6. (d) The normal logic stays readable, uncluttered by a check after every operation, and an error can be handled at whatever level makes sense rather than at the point it occurred.Either readability or the level of handling is acceptable.[1]

(a) separates normal and error paths; (b) files close either way; (c) loses specificity and hides bugs; (d) readability