---
title: "Handling multiple exceptions"
subject: "Object Oriented Programming"
chapter: 10
key: "semester_3/object_oriented_programming:multiple-exceptions"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/multiple-exceptions
site: BCT Engineering
---

# Handling multiple exceptions

Object Oriented Programming · Chapter 10: Exception Handling

- Asked in: 2074 Chaitra, 5 marks; 2072 Kartik, 5 marks (asked 2 times)
- Video: [Exception Handling Pt. 3: Creating and Handle Multiple Exceptions](https://www.youtube.com/watch?v=FRyLi4cHQfw) — theServinLab
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/multiple-exceptions
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-10

## Question

With suitable example explain how multiple exceptions are handled in C++.

## Short answer

A try block can throw different exception types. Multiple catch blocks are written in order; the first matching type handles it. A catch(...) block, placed last, catches any remaining exception.

## Answer

A program can have more than one kind of error. When a try block may throw different types of exceptions, **several catch blocks** are written after it, one for each type. The thrown value is compared with the catch handlers in order, and the first one with a matching type runs; the others are skipped. A `catch(...)` handler, placed last, catches any remaining exception.

Syntax:

```cpp
try { ... }
catch (type1 x) { ... }
catch (type2 y) { ... }
catch (...)     { ... }     // default handler (must be last)
```

#### Example

```cpp
#include <iostream>
using namespace std;

void test(int x) {
    try {
        if (x == 0) throw 0;              // int
        else if (x == 1) throw 'a';       // char
        else if (x == 2) throw 1.5;       // double
        else if (x == 3) throw "string";  // const char*
        cout << "No exception\n";
    }
    catch (int e)         { cout << "Caught int\n"; }
    catch (char e)        { cout << "Caught char: " << e << endl; }
    catch (double e)      { cout << "Caught double: " << e << endl; }
    catch (...)           { cout << "Caught some other exception\n"; }
}

int main() {
    for (int i = 0; i <= 4; i++)
        test(i);
    return 0;
}
```

**Output**

```text
Caught int
Caught char: a
Caught double: 1.5
Caught some other exception
No exception
```

`throw "string"` has type `const char*`, which matches none of the first three handlers, so `catch(...)` handles it.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How are different exception types handled from one try block?
   A. Use one catch only
   B. Use several try blocks only
   C. Use if statements instead
   D. Write one catch block per type after the try
   Answer: D. Multiple catch handlers follow the try block, one for each exception type. (Key point: Several catch blocks)

2. Which handler runs when the thrown type matches more than one?
   A. The first matching handler in order
   B. The last
   C. All of them
   D. None
   Answer: A. Handlers are compared in order, and the first match runs; the rest are skipped. (Key point: Matching order)

3. Where should catch(...) be placed?
   A. First
   B. Last
   C. Anywhere
   D. Inside try
   Answer: B. It matches everything, so it must come last or it would hide the others. (Key point: Default handler position)

4. Why is throw "string" handled by catch(...) in the example?
   A. Strings are not allowed
   B. catch(int) accepts strings
   C. Its type const char* matches none of int, char or double handlers
   D. It is converted to double
   Answer: C. The type const char* does not match the specific handlers, so the default one catches it. (Key point: Throwing a string)

## Related in Object Oriented Programming

- [Date of birth validation with exceptions](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adob-exception-program) — Object Oriented Programming, chapter 10

## Related in other subjects

- [Importance of error handling](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aerror-handling-importance) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Interrupts and handling with single INTR pin](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Ainterrupt-importance-single-intr) — Microprocessor, chapter 5
