Exception Handling

Exception handling separates error handling from normal logic: a try block runs risky code, catch blocks handle specific error types, and finally always runs for cleanup; an uncaught exception unwinds the call stack until a handler is found.

Key formulas & points

Skim these first — then read the full notes below.

  • finally runs even if try returns or throws
  • Catch the most specific exception type first
  • Do not use exceptions for ordinary control flow

Topic details

Introduction

This topic covers structured error handling. You throw and catch exceptions, order catch blocks from specific to general, guarantee cleanup with finally, distinguish checked from unchecked exceptions, and learn why exceptions should signal exceptional conditions rather than steer normal flow.

Key relations & formulas

Formulas (Indian textbook notation)

  • tryriskycatch(Typee)handlefinallycleanuptry { risky } catch (Type e) { handle } finally { cleanup }

Formulas (Indian textbook notation)

  • thrownewException(msg)unwindsthestacktothenearesthandlerthrow new Exception(msg) unwinds the stack to the nearest handler

Formulas (Indian textbook notation)

  • checkedexceptionsmustbedeclaredorcaught;uncheckedneednotbechecked exceptions must be declared or caught; unchecked need not be

Notation and sign conventions

Relation 1 —
tryriskycatchtry { risky } catch

Formulas (Indian textbook notation)

  • tryriskycatch(Typee)handlefinallycleanuptry { risky } catch (Type e) { handle } finally { cleanup }
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 2 —
thrownewExceptionthrow new Exception

Formulas (Indian textbook notation)

  • thrownewException(msg)unwindsthestacktothenearesthandlerthrow new Exception(msg) unwinds the stack to the nearest handler
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.
Relation 3 —
checkedexceptionsmustbedeclaredorcaught;uncheckedneednotbechecked exceptions must be declared or caught; unchecked need not be

Formulas (Indian textbook notation)

  • checkedexceptionsmustbedeclaredorcaught;uncheckedneednotbechecked exceptions must be declared or caught; unchecked need not be
Write this relation with symbols exactly as in Programming in ANSI C / OOP with C++ — E. Balagurusamy before substituting numbers. Examiners award partial marks for a correct setup even when arithmetic slips.

Concept in depth

Exceptions decouple the detection of an error from its handling, so the main logic stays readable while errors propagate up the call stack to code positioned to deal with them. When an exception is thrown, the runtime unwinds frames — running each finally block for cleanup like closing files — until it finds a matching catch. Because catch blocks are tried in order, a broad type before a narrow one would shadow it, so specific handlers come first. Checked exceptions force callers to acknowledge recoverable conditions at compile time, while unchecked (runtime) exceptions usually indicate programming bugs. Using exceptions for routine branching is discouraged because they are expensive and obscure intent.

Assumptions and validity limits

State assumptions explicitly before using any relation for exception handling — steady state, uniform properties, linear elastic material, ideal gas, incompressible flow, etc., as applicable.
Wrong assumptions invalidate the entire solution even when the formula is correct. In OOP viva and GATE descriptive questions, listing valid assumptions often earns separate marks.

Step-by-step problem approach

1. Read the question and list given data with SI units (common in OOP papers).
2. Draw a neat labelled diagram where applicable — examiners in Indian universities award diagram marks even when arithmetic slips.
3. Identify which relation from this topic applies to exception handling.
4. Use equation 1:
tryriskycatchtry { risky } catch
.
5. Use equation 2:
thrownewExceptionthrow new Exception
.
6. Substitute values, compute, and verify units and sign (direction).
7. State conclusion in one line — e.g. safe/unsafe, stable/unstable, feasible/infeasible.

Applications & exam relevance

Exception Handling appears in Java/C++ application development. In Indian it software curricula this topic is tested because it connects theory to classes, inheritance, and polymorphism.
GATE and semester exams often combine exception handling with earlier units — revise prerequisites before attempting mixed problems.
Industry interview panels sometimes ask: "Where did you use exception handling?" — answer with a lab, mini-project, or plant visit example if possible.

Common mistakes in exams

Students place a general catch before a specific one (making the specific unreachable), assume finally can be skipped (it runs even on return), and use exceptions for normal control flow. Forgetting that an uncaught exception propagates and terminates the program is another slip.

Quick revision checklist

Before attempting exception handling problems, confirm you can:
1. finally runs even if try returns or throws
2. Catch the most specific exception type first
3. Do not use exceptions for ordinary control flow
Revise the solved examples in Programming in ANSI C / OOP with C++ — E. Balagurusamy and one previous-year GATE or university paper for this unit.

Worked examples

Try the problem first — open the solution when you are ready to check.

finally execution

Problem

A try block returns 1, its finally block returns 2. What value does the method return?

Solution

It returns 2 — the finally block executes after the try’s return is prepared and its own return overrides it. This is exactly why returning from finally is discouraged.

Conceptual check — Exception Handling

Problem

In a OOP semester or GATE paper you are asked: "State the main assumption, the governing relation, and one practical consequence of exception handling." What should a complete answer include?

📖 Standard books (India)

  • Programming in ANSI C / OOP with C++E. Balagurusamy

    Read: Syllabus unit

    Standard Indian classroom programming text