Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 53: Exception Handling in C++ – Basics, Try-Catch, and Examples | Class 11, Chapter 5

Learn exception handling in C++ with Day 53 of Class 11 Computer Science, Chapter 5. Covers basics, try-catch, and practical examples with outputs.

Day 53 – Exception Handling in C++

Class 11 Computer Science – Chapter 5

What is Exception Handling? (Exception Handling क्या है?)

Exception handling in C++ is a mechanism to handle runtime errors and ensure the normal flow of the program. It uses the try, catch, and throw keywords.
C++ में Exception Handling एक तंत्र है जो रनटाइम त्रुटियों को संभालने और प्रोग्राम के सामान्य प्रवाह को सुनिश्चित करने के लिए उपयोग किया जाता है। इसमें try, catch, और throw कीवर्ड का उपयोग होता है।

Key Components of Exception Handling (Exception Handling के प्रमुख घटक)

  • try: Block of code to monitor for exceptions.
    try: वह कोड ब्लॉक जो अपवादों की निगरानी करता है।
  • throw: Used to throw an exception.
    throw: अपवाद को फेंकने के लिए उपयोग किया जाता है।
  • catch: Block of code that handles the exception.
    catch: वह कोड ब्लॉक जो अपवाद को संभालता है।

Syntax of Exception Handling (Exception Handling की संरचना)

try {
    // Code that may throw an exception
    throw exception_type;
} catch (exception_type e) {
    // Code to handle the exception
}

Examples of Exception Handling in C++ (C++ में Exception Handling के उदाहरण)

Example 1: Divide by Zero Exception

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 0;
    try {
        if (b == 0)
            throw "Division by zero error!";
        cout << "Result: " << a / b;
    } catch (const char* msg) {
        cout << "Exception: " << msg << endl;
    }
    return 0;
}

Output:

Exception: Division by zero error!

Example 2: Handling Multiple Exceptions

#include <iostream>
using namespace std;

int main() {
    try {
        throw 5.5; // Throwing a double exception
    } catch (int e) {
        cout << "Caught an integer: " << e << endl;
    } catch (double e) {
        cout << "Caught a double: " << e << endl;
    } catch (...) {
        cout << "Caught an unknown exception" << endl;
    }
    return 0;
}

Output:

Caught a double: 5.5

Advantages of Exception Handling (Exception Handling के लाभ)

  • Ensures the normal flow of the program.
    प्रोग्राम के सामान्य प्रवाह को सुनिश्चित करता है।
  • Separates error-handling code from the main logic.
    मुख्य तर्क से त्रुटि-संभालने वाले कोड को अलग करता है।
  • Makes programs robust and user-friendly.
    प्रोग्राम्स को मजबूत और उपयोगकर्ता-अनुकूल बनाता है।

Practice Questions

Multiple Choice Questions (MCQs)

  1. Which keyword is used to throw an exception in C++?
    (a) try | (b) catch | (c) throw | (d) None
  2. What is the purpose of the catch block?
    (a) Monitor code | (b) Handle exceptions | (c) Throw exceptions | (d) None
  3. Which of the following is not a part of exception handling in C++?
    (a) throw | (b) finally | (c) try | (d) catch
  4. What happens when an exception is not caught in C++?
    (a) Program terminates | (b) Error ignored | (c) Continues execution | (d) None
  5. What is the output of the following code?
    try {
        throw 10;
    } catch (int e) {
        cout << "Caught: " << e;
    }
            
    (a) Caught: 10 | (b) Error | (c) No output | (d) None
  6. Which keyword handles exceptions in C++?
    (a) catch | (b) throw | (c) try | (d) None
  7. What does the ... operator do in exception handling?
    (a) Catches any exception | (b) Catches only int | (c) Throws an exception | (d) None
  8. What is a runtime error in C++?
    (a) Syntax error | (b) Logical error | (c) Error during execution | (d) None
  9. Can a catch block handle multiple types of exceptions?
    (a) Yes | (b) No | (c) Only for specific exceptions | (d) None
  10. What is the output of this code?
    try {
        throw 3.14;
    } catch (double e) {
        cout << "Caught: " << e;
    }
            
    (a) Caught: 3.14 | (b) Error | (c) No output | (d) None

Answers to MCQs:

1: (c), 2: (b), 3: (b), 4: (a), 5: (a), 6: (a), 7: (a), 8: (c), 9: (a), 10: (a)

Short Answer Questions

  1. What is exception handling in C++?
    Answer: Exception handling is a mechanism to handle runtime errors and ensure normal program flow.
  2. Write the syntax for exception handling in C++.
    Answer:
    try {
        // Code that may throw exception
    } catch (type e) {
        // Handle exception
    }
    
  3. What is the purpose of the try block?
    Answer: To monitor code for exceptions.
  4. Write a program to demonstrate the use of multiple catch blocks in C++.
    Answer:
    try {
        throw 10.5;
    } catch (int e) {
        cout << "Caught integer";
    } catch (double e) {
        cout << "Caught double";
    }
    
  5. What happens if an exception is not caught in C++?
    Answer: The program terminates abnormally.

Long Answer Questions

  1. Explain the concept of exception handling with an example in C++.
  2. Discuss the advantages of using exception handling in programming.
  3. Write a program to handle divide by zero exception in C++.

Post a Comment