Skip to main content

Posts

Showing posts with the label Write a program to demonstrate the concept of rethrowing an exception.

Write a program to demonstrate the concept of rethrowing an exception.

 In C++, an exception can be rethrown to be caught by another catch block. This allows for more fine-grained handling of exceptions in a program. Here is an example program that demonstrates rethrowing an exception: #include <iostream> using namespace std; void divide(int a, int b) {     try {         if (b == 0) {             throw "Division by zero error";         }         else {             cout << "a/b = " << a/b << endl;         }     }     catch (const char* e) {         cout << "Exception caught in divide(): " << e << endl;         throw;  // rethrowing the same exception     } } int main() {     int x = 10, y = 0;     try {         divide(x, y);     }   ...