---
title: "Exception handling constructs"
subject: "Object Oriented Programming"
chapter: 10
key: "semester_3/object_oriented_programming:exception-handling-constructs"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/exception-handling-constructs
site: BCT Engineering
---

# Exception handling constructs

Object Oriented Programming · Chapter 10: Exception Handling

- Asked in: 2074 Chaitra, 3 marks; 2070 Chaitra, 3 marks; 2069 Chaitra, 5 marks; 2068 Chaitra, 3 marks (asked 4 times)
- Video: [Exception Handling in C++ Programming](https://www.youtube.com/watch?v=EyXXLpFriMc) — Simple Snippets
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/exception-handling-constructs
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-10

## Question

Explain about all Exception Handling constructs in C++ (try, catch, throw) with example.

## Short answer

try block encloses code that may throw an exception. throw raises an exception with a value. catch block handles a matching exception type; catch(...) catches any. Control resumes after the last catch if exception handled.

## Answer

An **exception** is a run-time error (divide by zero, array index out of range, memory failure). C++ handles exceptions with three keywords.

1. **`try`** – a block of code in which exceptions may occur. If an exception is thrown inside it, control leaves the block at once.
2. **`throw`** – raises (throws) an exception by giving a value or object of some type: `throw 10;`, `throw "error";`, `throw MyError();`
3. **`catch`** – a handler placed immediately after the try block. It catches an exception of a matching type and handles it. `catch(...)` catches any type.

Syntax:

```cpp
try {
    // code that may throw
    throw exception_value;
}
catch (type1 arg) {
    // handle type1
}
catch (...) {
    // handle everything else
}
```

#### Flow
```text
 try block --throw--> search matching catch --> handler runs
     |                       |                       |
  no exception        no match: terminate()     continue after
  (catch skipped)                               last catch block
```

#### Example

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

int main() {
    int a, b;
    cout << "Enter a and b: ";
    cin >> a >> b;
    try {
        if (b == 0)
            throw "Division by zero";    // throw
        cout << "a/b = " << a / b << endl;
    }
    catch (const char *msg) {            // catch
        cout << "Error: " << msg << endl;
    }
    cout << "Program continues\n";
    return 0;
}
```

For `b = 0` the output is `Error: Division by zero` followed by `Program continues`.

## Check yourself

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

1. What does throw do?
   A. Catches an exception
   B. Starts a try block
   C. Closes the program normally
   D. Raises an exception by giving a value or object
   Answer: D. throw raises the exception; control leaves the try block at once. (Key point: Role of throw)

2. Where must a catch handler be placed?
   A. Immediately after the try block
   B. Before the try block
   C. Inside main() only
   D. At the end of the file
   Answer: A. A catch handler follows its try block directly. (Key point: Catch placement)

3. What does catch(...) do?
   A. Catches only int
   B. Catches an exception of any type
   C. Rethrows automatically
   D. Is a syntax error
   Answer: B. The ellipsis handler catches any exception regardless of type. (Key point: catch(...))

4. What happens if a thrown exception has no matching catch?
   A. It is silently ignored
   B. The program continues after the try block
   C. terminate() is called
   D. The compiler fixes it
   Answer: C. With no matching handler, terminate() is called. (Key point: No matching catch)

## Related in Object Oriented Programming

- [Exception handling over conventional error handling](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-advantages) — Object Oriented Programming, chapter 10
- [Re-throwing an exception](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aexception-rethrow) — Object Oriented Programming, chapter 10

## Related in other subjects

- [Exception handling: OO vs conventional](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aexception-error-handling-oo-vs-conventional) — Object Oriented Analysis and Design, chapter 4
