exception

Prerequisite

What is Exception?

Exception is a problem or anomaly that a program faces in its execution.

 

What is Exception Handling and how it works?

Exception handling is a methodology that provides a mechanism to handle the exceptions or runtime problems of a program. It also a better way to deal with error that gives simplicity of code. This include try, throw and catch functions.

  • Try is block of code that identifies the exceptions.
  • Throw is used to throw and exception and is received by catch function.
  • Catch function is a block of code that is executed if the exception is occurred.

 

To have a better idea how it works, look at the simple example of the following code.

 

try{
    //program code 
}
catch()
{
    //code to execute if any problem occurs while executing the code if try block.
}

 

we can also use checkpoints to throw specific exceptions.

try{
    //some code
    if(number==0)
    {
        throw number;
    }
    //some code
}
catch(int number)
{
    cout<<"The number "<<number<<" is invalid.";
}

 

Question

Create a bankaccount class which contains deposit(), withdraw() and get_balance() member functions. Create a class named InsufficientFundsException inherited from Exception class to throw insufficient funds exception if you try to withdraw an amount greater than current balance. In demo class, use try, catch block to catch Exception thrown by withdraw method and display appropriate message sent from the Exception class.

 

#include<iostream>
using namespace std;

class InsufficientFundsException: public exception
{
    public:
        const char * what() const throw() 
        { 
            return "Insufficient Balance!"; 
        }
};


class bankaccount
{
    float balance=0.0;
    
    public:
    void get_balance()
    {
        cout<<endl<<endl<<"*****************Balance Inquiry*****************";
        cout<<endl<<"Your account Balance is: "<<balance;
    }
    
    void deposit()
    {
        cout<<endl<<"*****************Balance Deposit*****************";
        cout<<endl<<"Enter deposit amount: ";
        cin>>balance;
        cout<<"Your money is successfully deposited."<<endl;
    }
    
    void withdraw()
    {
        float amount=0;
        cout<<endl<<"*****************Money Withdraw*****************";
        cout<<endl<<"Enter withdraw amount: ";
        cin>>amount;
        try
        {
            if(amount>balance)
            {
                InsufficientFundsException obj;
                throw obj;
            }
            else
            {
                balance = balance-amount;
                cout<<amount<<" is withdrawn from the account.";
            }
        }
        catch(exception& e)
        {
            cout << e.what();
        }
        
    }
};


int main()
{
    bankaccount account1;
    account1.deposit();
    account1.withdraw();
    account1.get_balance();
    return 0;
    
}

 

The output of the above program:

*****************Balance Deposit*****************
Enter deposit amount: 1000
Your money is successfully deposited.

*****************Money Withdraw*****************
Enter withdraw amount: 500
500 is withdrawn from the account.

*****************Balance Inquiry*****************
Your account Balance is: 500
--------------------------------

 

And if we withdraw money more than the available balance then exception handling works.

*****************Balance Deposit*****************
Enter deposit amount: 1000
Your money is successfully deposited.

*****************Money Withdraw*****************
Enter withdraw amount: 12000
Insufficient Balance!

*****************Balance Inquiry*****************
Your account Balance is: 1000
--------------------------------

 

The program is created in c++. It has been tested with Bloodshed Dev C++ 5.9.2.

Leave a Reply

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