Divide by Zero Exception in C++
We use exception handling method to handle the divide by zero exception. Dividing a number with zero is generally mathematical error. We have to exception handling method to overcome this exception. In general, if we write the code without implementing exception handling then the code gives the output of infinite.
Example 1
#include <iostream>
using namespace std;
float Division(float num, float den)
{
return (num / den);
}
int main()
{
float numerator = 27.6;
float denominator = 0;
float result;
result = Division(numerator, denominator);
cout <<"The quotient of 27.6/0 is "
<< result << endl;
}
Output:
The quotient of 27.6/0 is inf
There are some different ways to handle this situation. Some of the ideas are as follows:
1) Using the runtime_error class
There is a standard library class exception called runtime_error class. This runtime_error class is used to handel the runtime error during presentation of header file exception. Now we are going to try the runtime_error class with the above code and see what we get in the output. There is a try block that called the division function. If the quotient returns zero value then the devison function is being checked by the runtime_error class. The catch block caught that exception and gives the output of which prints the message “Exception occurred”. There is an another function called what() function that is used to defined in stdexcept header file. This function is used to handel the exception.
Example 2
#include <iostream>
#include <stdexcept>
using namespace std;
float Division(float num, float den)
{
if (den == 0) {
throw runtime_error("Math error: Attempted to divide by Zero\n");
}
return (num / den);
}
int main()
{
float numerator, denominator, result;
numerator = 27.6;
denominator = 0;
try {
result = Division(numerator, denominator);
cout <<"The quotient is "
<< result << endl;
}
catch (runtime_error& e) {
cout <<"Exception occurred"<< endl
<< e.what();
}
}
Output:
Exception occurred
Math error: Attempted to divide by Zero
2) Using User defined exception handling
There is another class function that is used to inherit publically from runtime_error class. In that class, we define a constructor that shows the message “Math error: Attempted to divide by Zero”. When we call the object then this message will appear in the output. Then the catch block catches the exception and shows the message the message “Exception occurred”.
Example 3
#include <iostream>
#include <stdexcept>
using namespace std;
class Exception : public runtime_error {
public:
Exception()
: runtime_error("Math error: Attempted to divide by Zero\n")
{
}
};
float Division(float num, float den)
{
if (den == 0)
throw Exception();
return (num / den);
}
int main()
{
float numerator, denominator, result;
numerator = 27.6;
denominator = 0;
try {
result = Division(numerator, denominator);
cout <<"The quotient is "<< result << endl;
}
catch (Exception& e) {
cout <<"Exception occurred"<< endl
<< e.what();
}
}
Output:
Exception occurred
Math error: Attempted to divide by Zero