Basics of Exception Handling in C++

 Exception Handling in C++



In this article we will look at the basic syntax of exception handling with the code for the execution of handling exceptions by starting off with a simple example. 

We will also look into a simple project idea. Exception handling in a banking management system.

Division by zero is indetermined, so a basic division code with an input of denominator being 0 would say core dumped. This is because this is an exception.

#include<bits/stdc++.h>
using namespace std;

//function for handling exceptions
float division(int a, int b)
{
    
    //denominator can not be 0
    if(b==0)
    
        //if denominator is 0
        //then, exit with this statement
        throw "Division by 0";
        
    //if the conditions are met properly
    //then return the value
    return a/b;
    
}

int main()
{
    int a,b,res;
    cin>>a>>b;
    
    //"try" to do the following under the
    //criteria given in the function block
    try
    {
        
        //store the result in 'res'
        //happens if the conditions are met
        res=division(a,b);
        cout<<endl<<res;
    }
    
    //incase of the "throw" output being executed
    //so some condition is not met, then
    catch(const char* e)
    {
        
        //print whichever "throw" statement
        //correspods to the unmet condition
        cerr<<endl<<e;
    }
}

In the above code, if the exception is encountered, then the return statement will not be executed. However if the conditions are met properly, then only the return statement will be executed.

Feel free to copy this code and play along with different values.

Now lets go for the project. A simple exception handling code for a banking management system.
Let's first look at the code.

#include<bits/stdc++.h>
using namespace std;

float bank(int exist, int deposite, int withdraw)
{
    //incase initially exist is 0, but we deposite
    exist+=deposite;
    
    //any negative values must be terminated
    if(deposite<0||withdraw<0||exist<0)
        throw "Enter proper values";
    
    //all 0's means not task exists
    else if((deposite==0&&withdraw==0) || exist==0)
        throw "No task is to be done";
    
    //withdraw can not be more than existing amount    
    else if(withdraw>exist)
        throw "Not enough existing money";
    
    //for final result of exist    
    exist-=withdraw;
    
    return exist;
}

int main()
{
    int exist,deposite,withdraw;
    cin>>exist>>deposite>>withdraw;
    
    //try block of the code
    try
    {
        exist=bank(exist,deposite,withdraw);
        cout<<endl<<exist;
    }
    
    //catch block of the code
    catch(const char* e)
    {
        cerr<<endl<<e;
    }
}

Now, as explained in the video. The conditions for the variables were intially made.
In every if loop in the function block, we place the impossible conditions and a respective throw statement.

In case the user input satisfy one of these conditions, then the throw output is executed. Else, we simply allow the transaction to take place.

Hope this gets you started in C++, exception handling.

Comments