- 2070 Bhadra · 5+5 marks
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
ifstatements. - 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
finallyor 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"); }
| Point | Conventional | Object-oriented |
|---|---|---|
| Error signal | Return code / global flag | Exception object |
| Checking | Manual, after each call | Automatic propagation, catch |
| Code structure | Mixed with normal code | Separate try/catch blocks |
| Information | A number | Object with message, type, stack trace |
| Classification | None | Exception class hierarchy |
| Forgetting to handle | Silently ignored | Program stops (unchecked) or compile error (checked) |
| Cleanup | Manual | finally, destructors |
The OO method is therefore cleaner, safer, and more informative.
✓ Answer checked by Claude Sonnet 5.5
Related questions
- OO design cycle vs conventional cycle · Object Oriented Analysis and Design, chapter 1
- Object oriented system and core concepts · Object Oriented Analysis and Design, chapter 1
- Algorithmic vs object-oriented decomposition · Object Oriented Analysis and Design, chapter 1
- Importance of error handling · Object Oriented Analysis and Design, chapter 4 · asked 2×
- Interface vs implementation · Object Oriented Analysis and Design, chapter 4 · asked 2×
- Structural model vs implementation model · Object Oriented Analysis and Design, chapter 1
- Exception handling over conventional error handling · Object Oriented Programming, chapter 10
- Exception handling constructs · Object Oriented Programming, chapter 10 · asked 4×
- Compare procedural and object oriented programming · Object Oriented Programming, chapter 1
- Exception and error testing · Software Engineering, chapter 8
All Object Oriented Analysis and Design chapter 4 questions →