Exception Handling
What is Exception Handling:
Exception is a problem of that Aries during execution of a program. It is damage program and stop execution. It is occurred when we divided by zero or the value is undefined. An exception is responsible to run the program successfully without any problem or Error.
Syntax
try {
throw exception;
}
catch (type argument) {
// Code
}
Example
#include<iostream>
using namespace std;
int main () {
double x;
cout << " Enter Value of X : " ;
cin >> x;
try {
if (x != 0) {
cout << " Value is: " << 1/x;
}
else {
throw (x);
}
}
catch (double k) {
cout << " Division By Zero Condition! is not satisfy " ;
}
return 0;
}
Output
Example 2
#include <iostream>
using namespace std;
int main () {
double x, y, diff;
cout << " Enter Value of X : " ;
cin >> x;
cout << " Enter Value of X : " ;
cin >> y;
diff = y-x;
try {
if (x != 0) {
cout << " Value is: " << 1/diff;
}
else {
throw (diff);
}
}
catch (double diff) {
cout << " Division By Zero Condition! Not Satisfy " ;
}
return 0;
No comments