Catching all exception block in Exception Handling
The catch statement catches an exception whose type match with the type of catch argument. When it is caught, The code in the catch block executed. Code for handling exception is included in catch blocks, Catching all exception block looks a function definition and is of the form:
The catch statement catches an exception whose type match with the type of catch argument. When it is caught, The code in the catch block executed. Code for handling exception is included in catch blocks, Catching all exception block looks a function definition and is of the form:
catch(...)
{
// statement for
// managing exceptions
}
The type indicates the type of exception that catch block handles. The parameter arg is an optional parameter name. If the parameter in the catch statement is named, then the parameter can be used in the exception handling code. After executing the handler the control goes to the statement immediately following catch block. Due to mismatch if any exception is not caught, abnormal program termination will occur.
Program for catching all exception block:
#include<iostream>
using namespace std;
void testcase(int x)
{
try
{
if(x==0)
{
//throwing an int type of exception.
throw x;
}
else if(x==1)
{
// throwing a char type of exception.
throw 'x';
}
else if(x==-1)
{
// throwing a double type of exception.
throw 1.0;
}
}
catch(...)
{
cout<<"End of multiple try – catch block.\n";
}
}
int main()
{
int a;
cout<<"Enter any number\n";
cin>>a;
testcase(a);
}
Post A Comment:
0 comments: