CATCHING ALL EXCEPTIONS
Q20. Explain the process of catching all exceptions.
Answer:
Catching Multiple Exceptions
In C++, a user can catch all exceptions simultaneously Inorder to do this, a single catch block is defined for catching all the exceptions thrown by using different throw statements. This catch block is of generic type.
Syntax
catch
{
//statements fur handling various exceptions
}
Program
#include<iostream>
using namespace std;
void number(int p) //defining a function called number with
single argument
{
try
{
if(p==0)
{
//if p is equal to zero throw exception
throw 'p';
}
else
if(p>0)
{
throw 'q';
}
else
if(p<0)
{
throw 'r';
}
cout<<"Try Block \n";
}
catch(char ch) //defining single catch block
{
cout<<"\n Exception caught\n";
}
}
int main()
(
number(0);
number(5);
number(-2);
return 0;
}
Output