Objective: Demonstrate a C++ program to implement the catching of multiple exceptions.
Code:
#include <iostream>
#include <stdexcept> // For out_of_range exception
using namespace std;
int main() {
try {
int choice;
cout << "Enter a number (1 for division by zero, 2 for invalid index access, or any other number for no exception): ";
cin >> choice;
if (choice == 1) {
int a, b;
cout << "Enter two integers (second integer should be zero to trigger the exception): ";
cin >> a >> b;
if (b == 0) {
// Throw a division by zero exception
throw "Division by zero error!";
} else {
cout << "Result of division: " << a / b << endl;
}
} else if (choice == 2) {
int index;
cout << "Enter an index (valid indices are 0 to 4): ";
cin >> index;
int arr[5] = {10, 20, 30, 40, 50};
if (index < 0 || index >= 5) {
// Simulate an array out of bounds error
throw out_of_range("Index out of bounds error!");
} else {
cout << "Value at index " << index << " is " << arr[index] << endl;
}
} else {
cout << "No exception occurred!" << endl;
}
} catch (const char* msg) {
// Catch block for string (char*) exceptions
cout << "Caught an exception: " << msg << endl;
} catch (const out_of_range& e) {
// Catch block for out_of_range exceptions
cout << "Caught an exception: " << e.what() << endl;
} catch (...) {
// Generic catch block for any other exceptions
cout << "Caught an unknown exception!" << endl;
}
return 0;
}
Output:
No comments:
Post a Comment