Handling Divide by Zero Exception in C++

Today we’ll learn how to handle divide by zero exception in C++.

Exception handling is an important feature in programming and software development. It tells the compiler how to deal with flaws during compile time. Prior knowledge on Exception handling in C++ will be helpful.

Divide by zero exception handling

This exception is most common as it involves basic math. Any number upon division with zero is not defined.

A try-catch block can save it all.

  • try block executes and checks the logic
  • It throws the exception to the catch block
  • catch block catches the exception and provides the necessary result.

Assume the user enters a=1 and b=0 and executes operation a/b. Since it is not defined, the compiler stops there. Now the operation will execute in the try block. The catch block provides the solution, which the compiler can execute.

Implementation

Implementation has two examples with different logic. The first logic is basic division, while the second logic has an arithmetic calculation. In case one is executing the code on any online platform, set it on interactive mode for good output.

example 1:

The user has to enter two variables var1 and var2. var1 is dividend and var2 is the divisor. If var2=0, exception arises. The code handles it as follows:

#include<iostream>
using namespace std;
int main()
{
int var1,var2;
float var3;
cout<<"enter the dividend:";
cin>>var1;
cout<<"\n";
cout<<"enter the divisor:";
cin>>var2;
cout<<"\n";
//exception handling begins here
try //try block
{
if(var2!=0) //checking if divisor is zero
{
var3=var1/var2;
cout<<"outcome :"<<var3;
}
else
{
throw(var2); //throwing the exception found
}
}
//catch block
catch(int exc)
{
cout<<"division by zero is not possible. Please try again with different value of variables"<<i;
}
}

Outcome:

The outcome of the above code is as follows:

Enter the divident : 9
Enter the divisor: 0
division by zero is not possible. Please try again with different value of variables

example 2:

Here the user has to enter three variables – var1, var2, var3. var3 is the numerator. The denominator performs a subtraction of var1-var2, and this value is the divisor for var3.

#include<iostream>
using namespace std;
int main()
{
int var1,var2,var3;
float var4;
cout<<"denominator:"<<"\n";
cout<<"enter the minuend:";
cin>>var1;
cout<<"\n";
cout<<"enter the subtrahend:";
cin>>var2;
cout<<"\n";
cout<<"numerator:"<<"\n";
cout<<"enter numerator:";
cin>>var3;
cout<<"\n";
//exception handling begins here
try //try block
{
if((var1-var2)!=0)
{
var4=var3/(var1-var2);
cout<<"outcome :"<<var4;
}
else
{
throw(var1-var2); //throwing the exception found
}
}
//catch block
catch(int exc)
{
cout<<"division by zero is not possible. Please try again with different value of variables";
}
}

Output:

The output of the above code is as follows:

denominator:
Enter the minuend:3
Enter the subtrahend: 3
Numerator:
Enter the numerator: 45
division by zero is not possible. Please try again with different value of variables

More examples of maths can cause such an exception. Multiplying with zero can also cause this, or even a combination of +,-,*.

For further details on C++ syntaxes, concepts and related methods, refer the link: C++ reference

Leave a Reply

Your email address will not be published. Required fields are marked *