Exceptional Handling
Exceptional Handling
Exception Handling
- Run time error is called as an exception. C++ has a mechanism to handle exceptions. The exception handling mechanism of C++ uses the following keywords: try, throw and catch.
Examples of exception
- Divide by zero
- Accessing array out of bound
- Running out of memory or disk space
Types of Exceptions
- Synchronous exception
- Asynchronous exception
1)The exceptions that are caused by events under the control of the program are called as synchronous exceptions.
Example: Divide by zero, Accessing array out of bound, Running out of memory or disk space
2) The exceptions that are caused by events beyond the control of the program are called as
synchronous exceptions. Example: Keyboard Interrupts.
The exception handling mechanism in C++ is designed to handle only synchronous exceptions.
Example of Exception Handling
void main()
{
int a,b;
cin>>a>>b;
try
{
if(b!=0)
{
cout<<a/b<<"\t";
}
else
{
throw b;
}
}//end of try
catch(int i)
{
cout<<“\tException caught "<<i;
}
}
}//end of main
Output: 12 0 Exception caught 0
Comments
Post a Comment