Skip to main content

Exception handling: OO vs conventional

Short answer

Conventional error handling uses return codes and manual checks, mixing error logic with normal code. Object-oriented handling throws exception objects that propagate automatically up the call stack, caught in separate try/catch blocks, with finally for cleanup. OO provides richer error information, hierarchy, and safer, cleaner code.

What is exception and error handling in the context of system implementation? How does it differ from other conventional method versus the object-oriented method based implementation?

Exception and error handling

An error is a wrong condition or result in a program (for example a wrong value, a logical mistake, resource failure). An exception is an unexpected event during the run that disrupts the normal flow of instructions (division by zero, file not found, null reference, network down). Error handling is the set of techniques that detect these situations and respond to them: recover, report or stop in a safe state, without losing data or crashing.

Conventional method (procedural: C, Pascal)

  • Functions return error codes (-1, NULL, errno) or set global flags.
  • The caller must check after every call with if statements.
  • Normal logic and error logic are mixed, so the code is long and hard to read.
  • Easy to forget the check; the error is then silently ignored.
  • Error cannot easily be passed up through several levels.
FILE *f = fopen("a.txt", "r");
if (f == NULL) { printf("error\n"); return -1; }

Object-oriented method (Java, C++, C#)

  • Errors are objects of exception classes (organised in an inheritance hierarchy, e.g. Throwable -> Exception -> IOException).
  • The code that detects the problem throws an exception; the one that knows how to handle it catches it with try/catch/finally.
  • Exceptions propagate up the call stack automatically.
  • Resources are cleaned up in finally or by destructors/try-with-resources.
try {
    FileReader f = new FileReader("a.txt");
} catch (FileNotFoundException e) {
    System.out.println("File missing");
} finally { System.out.println("done"); }
PointConventionalObject-oriented
Error signalReturn code / global flagException object
CheckingManual, after each callAutomatic propagation, catch
Code structureMixed with normal codeSeparate try/catch blocks
InformationA numberObject with message, type, stack trace
ClassificationNoneException class hierarchy
Forgetting to handleSilently ignoredProgram stops (unchecked) or compile error (checked)
CleanupManualfinally, destructors

The OO method is therefore cleaner, safer, and more informative.

✓ Answer checked by Claude Sonnet 5.5

All Object Oriented Analysis and Design chapter 4 questions →