How to catch all the exceptions in C++?

C++ProgrammingServer Side Programming

Exceptions are the problems which arise at the time of execution of program. It is an event which is thrown at runtime. It protects the code and run the program even after throwing an exception. Exception handling is used to handle the exceptions. We can use try catch block to protect the code.

Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.

Here is an example of catching all the exceptions in C++ language,

Example

 Live Demo

#include <iostream>
using namespace std;

void func(int a) {
   try {
      if(a==0) throw 23.33;
      if(a==1) throw 's';
   } catch(...) {
      cout << "Caught Exception!\n";
   }
}
int main() {
   func(0);
   func(1);
   return 0;
}

Output

Here is the output

Caught Exception!
Caught Exception!
raja
Published on 24-Oct-2018 10:37:01
Advertisements