Write a C++ program to demonstrate
exception handling. You may take dividing by zero as a
case of exception.
Ans
#include <iostream>
#include <stdexcept>
inline int intDiv (int numerator, int denominator) {
if (denominator ==
0)
throw
std::overflow_error("Divide by zero exception");
return numerator /
denominator;
}
int main (void) {
int i = 42;
try {
i = intDiv (10,
2);
} catch
(std::overflow_error e) {
std::cout
<< e.what() << " -> ";
}
std::cout <<
i << std::endl;
try {
i = intDiv (10,
0);
} catch
(std::overflow_error e) {
std::cout
<< e.what() << " -> ";
}
std::cout <<
i << std::endl;
return 0;
No comments:
Post a Comment