Exceptions are errors that occur at runtime. They are caused by a wide variety of exceptional circumstances, such as running out of memory, not being able to open a file, trying to initialize an object to an impossible value, or using an out-of-bounds index to a vector. An exception is an error or an expected event. The exception handler is a set of codes that execute when an exception occurs. Exception handling is one of the most recently added features in C++ language.
Exception handling in C++ provides a better method by which the caller of a function can be informed that some error condition has occurred. The following keywords are used for error handling in C++ language.
1. Try block
2. Catch block
3. Throw statement
An exception also known as runtime errors because these are occurs at runtime. For example, there may be an abnormal condition or unexpected behavior when we try to divide a number by zero or an array is accessed outside of its bounds, or when required memory is not available, etc.
Program 1. for Demo of Exception:
#include<iostream.h>
int main()
{
Int z, y, x;
cout<<”Enter any two numbers”<<endl;
cin>>x>>y;
z=x/y;
cout<<x<<”/”<<y<<”=”<<z;
}
Output 1
Enter any two numbers
10
2
10/2 = 5
Output 2
Enter any two numbers
20
0
Not produce any output but will terminate abnormally and produce an exception divide-error.
Post A Comment:
0 comments: