DEV Community

Murat Gungor
Murat Gungor

Posted on • Originally published at dzone.com

Need for Exceptions in Code

Exception

Assume you have the following Bank class. In this code, the withdraw method is our main focus. Its job is to deduct a given amount from the balance and then return the updated balance.

       class Bank {
              public:
                  Bank(double balance = 0) {
                     this->balance = balance;
                  }
                  double withdraw(double amount) {
                     balance = balance - amount;
                     return balance;
                  }
              private:
                  double balance;
       };

As you notice, it can return only a double. I would say that this function can only speak in "double" language, poor function.


In other words, currently, withdraw function can speak only one language, the"return type" language.

There is a banking rule waiting to be implemented, which is "an account overdraft should not be allowed". For this rule, there is one solution we can silently return the current balance without deducing the amount.

    double withdraw(double amount) {
        if (balance < amount)
            return balance;
        balance = balance - amount;
        return balance;
    }

But, this solution does not inform the client (in this case the main method) about the reason why the balance is not changed; actually, it says nothing to the client. The client has to figure out whether the request granted or not.

If the client notices the balance did not change, it can only guess!

Possible reasons could be:

  1. There is a daily limit?
  2. The balance is not adequate?
  3. There is a minimum balance rule?

But poor function cannot express itself. It cannot speak any other language: It can only speak (return) in "double" language.

Look at the code below. Now, our withdraw function can speak "string" language as well. Now, it can express more than before, yay!!

    class Bank {
        public:
        Bank(double balance = 0) {
            this->balance = balance;
        }
        double withdraw(double amount) {
            if (balance < amount)
                throw string("Balance is not enough");
            balance = balance - amount;
            return balance;
        }

        private:
            double balance;

    };

    int main()
    {
        Bank checking(100);
        try {       
            cout << "Balance after withdraw: $" << checking.withdraw(120) << endl;
            cout << "Thank you" << endl;
        }
        catch (string exceptionMessage) {
            cout << "Error Ocurred: " << exceptionMessage << endl;
        } 
    }

Exception

If there is a daily limit, we can now implement it, and we can inform the client if it's violated. Let's look at the code below.

const int DAILY_LIMIT = 30;

class Bank {
public:
    Bank(double balance = 0) {
        this->balance = balance;
    }
    double withdraw(double amount) {
        if ( amount > DAILY_LIMIT)
            throw string("Daily limit is exceeded");
        if (balance < amount)
            throw string("Balance is not enough");      
        balance = balance - amount;
        return balance;
    }
private:
    double balance;
};

Exception keeps your client in the know

Your client has a right to know why something has not happened, or where did the client make a mistake. Exception enables your client in the know.

Exception enables your function to express more than the "return type" value.


Exception enables your function to speak in any language

Top comments (1)

Collapse
 
siy profile image
Sergiy Yevtushenko

Usually it's better to avoid using exceptions. And use functional programming inspired approaches (Either monad) for error reporting and propagation.