Last Updated : 11 Jul, 2025
We use Exception Handling to overcome exceptions occurred in execution of a program in a systematic manner.
Dividing a number by Zero is a mathematical error (not defined) and we can use exception handling to gracefully overcome such operations. If you write a code without using exception handling then the output of division by zero will be shown as infinity which cannot be further processed.
Consider the code given below, the Division function returns the result of numerator divided by denominator which is stored in the variable result in the main and then displayed. This Division function does not have any rumination for denominator being zero.
C++
// Program to show division without using
// Exception Handling
#include <iostream>
using namespace std;
// Defining function Division
float Division(float num, float den)
{
// return the result of division
return (num / den);
} // end Division
int main()
{
// storing 12.5 in numerator
// and 0 in denominator
float numerator = 12.5;
float denominator = 0;
float result;
// calls Division function
result = Division(numerator, denominator);
// display the value stored in result
cout << "The quotient of 12.5/0 is "
<< result << endl;
} // end main
The quotient of 12.5/0 is inf
We can handle this exception in a number of different ways, some of which are listed below
// Program to depict how to handle
// divide by zero exception
#include <iostream>
#include <stdexcept> // To use runtime_error
using namespace std;
// Defining function Division
float Division(float num, float den)
{
// If denominator is Zero
// throw runtime_error
if (den == 0) {
throw runtime_error("Math error: Attempted to divide by Zero\n");
}
// Otherwise return the result of division
return (num / den);
} // end Division
int main()
{
float numerator, denominator, result;
numerator = 12.5;
denominator = 0;
// try block calls the Division function
try {
result = Division(numerator, denominator);
// this will not print in this example
cout << "The quotient is "
<< result << endl;
}
// catch block catches exception thrown
// by the Division function
catch (runtime_error& e) {
// prints that exception has occurred
// calls the what function
// using runtime_error object
cout << "Exception occurred" << endl
<< e.what();
}
} // end main
Exception occurred Math error: Attempted to divide by Zero
// Program to depict user defined exception handling
#include <iostream>
#include <stdexcept>
// For using runtime_error
using namespace std;
// User defined class for handling exception
// Class Exception publicly inherits
// the runtime_error class
class Exception : public runtime_error {
public:
// Defining constructor of class Exception
// that passes a string message to the runtime_error class
Exception()
: runtime_error("Math error: Attempted to divide by Zero\n")
{
}
};
// defining Division function
float Division(float num, float den)
{
// If denominator is Zero
// throw user defined exception of type Exception
if (den == 0)
throw Exception();
// otherwise return the result of division
return (num / den);
} // end Division
int main()
{
float numerator, denominator, result;
numerator = 12.5;
denominator = 0;
// try block calls the Division function
try {
result = Division(numerator, denominator);
// this will not print in this example
cout << "The quotient is " << result << endl;
}
// catch block catches exception if any
// of type Exception
catch (Exception& e) {
// prints that exception has occurred
// calls the what function using object of
// the user defined class called Exception
cout << "Exception occurred" << endl
<< e.what();
}
} // end main
Exception occurred Math error: Attempted to divide by Zero
// Program to depict Exception Handling
// Using stack unwinding
#include <iostream>
#include <stdexcept>
using namespace std;
// defining the CheckDenominator function
float CheckDenominator(float den)
{
// if denominator is zero
// throw exception
if (den == 0) {
throw runtime_error("Math error: Attempted to divide by zero\n");
}
else
return den;
} // end CheckDenominator
// defining Division function
float Division(float num, float den)
{
// Division function calls CheckDenominator
return (num / CheckDenominator(den));
} // end Division
int main()
{
float numerator, denominator, result;
numerator = 12.5;
denominator = 0;
// try block calls the Division function
try {
result = Division(numerator, denominator);
// This will not print in this example
cout << "The quotient is "
<< result << endl;
}
// catch block catches exception if any
catch (runtime_error& e) {
// prints that exception has occurred
// calls the what function using object of
// runtime_error class
cout << "Exception occurred" << endl
<< e.what();
}
} // end main
Exception occurred Math error: Attempted to divide by zero
// Program to depict use of try catch block
#include <iostream>
#include <stdexcept>
using namespace std;
// defining CheckDenominator
float CheckDenominator(float den)
{
if (den == 0)
throw "Error";
else
return den;
} // end CheckDenominator
int main()
{
float numerator, denominator, result;
numerator = 12.5;
denominator = 0;
// try block
try {
// calls the CheckDenominator function
// by passing a string "Error"
if (CheckDenominator(denominator)) {
result = (numerator / denominator);
cout << "The quotient is "
<< result << endl;
}
}
// catch block
// capable of catching any type of exception
catch (...) {
// Display a that exception has occurred
cout << "Exception occurred" << endl;
}
} // end main
Exception occurred
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4