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)
- Which keyword is used to throw an exception in C++?
(a) try | (b) catch | (c) throw | (d) None - What is the purpose of the
catchblock?
(a) Monitor code | (b) Handle exceptions | (c) Throw exceptions | (d) None - Which of the following is not a part of exception handling in C++?
(a) throw | (b) finally | (c) try | (d) catch - What happens when an exception is not caught in C++?
(a) Program terminates | (b) Error ignored | (c) Continues execution | (d) None - 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 - Which keyword handles exceptions in C++?
(a) catch | (b) throw | (c) try | (d) None - What does the
...operator do in exception handling?
(a) Catches any exception | (b) Catches only int | (c) Throws an exception | (d) None - What is a runtime error in C++?
(a) Syntax error | (b) Logical error | (c) Error during execution | (d) None - Can a
catchblock handle multiple types of exceptions?
(a) Yes | (b) No | (c) Only for specific exceptions | (d) None - 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
- What is exception handling in C++?
Answer: Exception handling is a mechanism to handle runtime errors and ensure normal program flow. - Write the syntax for exception handling in C++.
Answer:try { // Code that may throw exception } catch (type e) { // Handle exception } - What is the purpose of the
tryblock?
Answer: To monitor code for exceptions. - 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"; } - What happens if an exception is not caught in C++?
Answer: The program terminates abnormally.
Long Answer Questions
- Explain the concept of exception handling with an example in C++.
- Discuss the advantages of using exception handling in programming.
- Write a program to handle divide by zero exception in C++.
Post a Comment