Exception Handling: Bibha Sthapit Asst. Professor Ioe, Pulchowk Campus
Exception Handling: Bibha Sthapit Asst. Professor Ioe, Pulchowk Campus
Exception Handling
BIBHA STHAPIT
ASST. PROFESSOR
IOE, PULCHOWK CAMPUS
Introduction
• Two common types of error in a program are:
– 1) Syntax error (arises due to missing semicolon, comma, and
wrong program constructs etc)
– 2) Logical error (wrong understanding of the problem or wrong
procedure to get the solution)
3
Exception handling mechanism
• C++ exception handling mechanism is basically built upon
three keywords namely, try, throw and catch.
6
Exception handling mechanism
• The general form of try and catch block is as follows:
try
{
/* try block;
throw exception*/
}
main()
Output:
{ int a,b;
1st Run:
cout<<“Enter two numbers (num/den):";
cin>>a>>b;
try Enter two numbers (num/den):8 2
{ if (b==0) Result=4
throw b;
else
2nd Run:
cout<<“Result=“<<a/b;
}
catch(int x) Enter two numbers (num/den):4 0
{ cout<<"Denominator cannot be zero"; } Denominator cannot be zero
}
8
2. Exception generated by function
15
int main() Output:
void divide(int x, int y)
{ { Inside main
cout<<" Inside Function \n"; cout <<"Inside main \n"; Inside Function
try try
Result =5
{ if (y== 0) { divide(10,2);
divide(20,0); End of function
throw y; //throwing int
else }
cout<<"Result =" << x/y<<endl; catch (int) Inside Function
} {
Caught int inside a function
catch(int) //Catch a int cout <<"Caught int inside main \n";
Caught int inside main
{ }
cout <<"End of main\n "; End of main
cout<<"Caught int inside a function \n";
throw; //re-throwing int return 0;
} }
cout<<"End of function\n\n";
}
16
Exception specification for function
• In some cases it may be possible to restrict a function to throw only certain
specified exceptions. This is achieved by adding a throw list clause to function
definition. The general form of using an exception specification is:
Ret_Type function (arg-list) throw (type-list)
{
// function body
}
• The type list specifies the type of exceptions that may be thrown. Throwing any
other type of exception will cause abnormal program termination. To prevent a
function from throwing any exception, it can be done by making the type list empty
like throw(); in the function header line.
• Similarly, include throw(…); in function header line to throw all types of exceptions.
17
void test(int n)throw(double,int) main() Output:
{ { cout<<"Inside main\n";
cout<<"Inside function\n"; try Inside main
if(n==0) testing throw restrictions
{ cout<<"testing throw restrictions\n";
throw 1; Inside function
if (n>0) test(23);
terminate called after
throw '1'; test(0); throwing an instance of
if (n<0) 'char'
}
throw 1.0; catch(int i)
cout<<"End of function\n";
{ cout<<"Integer exception caught\n"; }
}
catch(char ch)
{ cout<<"character exception caught\n"; }
catch(double f)
{ cout<<"Double exception caught\n"; }
cout<<"End of main\n"; 18
}
Exception with argument
class numbers
void divide()
{
{
int a, b;
cout<<"enter two numbers (num/den):";
public:
cin>>a>>b;
class div_zero// Exception class
if (b==0)
{
throw div_zero(b,"Divide by zero");
public:
else
int err_no;
cout<<"Result="<<a/b<<endl;
char err_name[50];
div_zero(int num, char *name)
}
{ err_no=num;
};
strcpy(err_name,name);
}
19
};
Exception with argument(contd.)
main() Output:
{
numbers n; enter two numbers (num/den):65 5
try{ Result=13
n.divide(); enter two numbers (num/den):65 0
n.divide(); Exception Caught:0Divide by zero
}
catch(numbers::div_zero z)
{ cout<<"Exception Caught:"<<z.err_no<<z.err_name;
}
} 20
Handling uncaught exception
• If no handler at any level catches the exception, the special library
function terminate( ) (declared in the <exception> header) is automatically
called. By default, terminate( ) calls the library function abort( ) , which abruptly
exits the program.
{ }
Output:
cout<<"Uncaught exception found\n";
Inside main
abort(); Inside function 22
} Uncaught exception found
Handling unexpected exception
• The special function unexpected( ) is called when we throw something other than
what appears in the exception specification. The default unexpected( ) calls
the terminate( ) function.
• This function is provided so that the unexpected handler can be explicitly called by
a program, and works even if set_unexpected has not been used to set a
custom unexpected handler (calling terminate in this case)
23
main()
void test(int n)throw(int)
{ set_unexpected(my_unexpected_handler);
{ cout<<"Inside main\n";
cout<<"Inside function\n"; try
if(n<0) throw 1; {
main() {
{ switch(ch)
int item,sz; {
int ch=1; {
cout<<"\n\n\tMENU\n1.PUSH\n2.POP\n }
{ }
cout<<"\n***Stack Empty***\n"; break;
} case 4:
break; exit(0);
}
cout<<"\nEnter your choice:";
cin>>ch;
}while(ch<5); 27
}