Question Custom Exceptions

Nas

Member
Joined
Oct 13, 2018
Messages
5
Programming Experience
Beginner
I can't get my code (below) to execute the exceptions!
1- First, the bank account is empty.
2- 1500 deposit is made
3- Attempt to withdraw 1050 but the overdraft limit is 1000
4- Since the the amount to withdraw is greater than the overdraft limit the OverdraftEx exception should display my message (but it is not).
5- Then an attempt to withdraw 1000 which should be successful and the balance should become 500
6- Now, an attempt to withdraw 50 but second exception message DailyLimitEx should display which is exceeding the daily limit, because the Daily withdraw limit is 1000 and it has already been withdrawn for the day, again this exception is not getting displayed.
7- Then, End of the Day will display which is resetting the DailyLimit back to 0.
8- Then an attempt to withdraw 50 is allowed because it is a new day.
I can't get the exceptions' messages to display.
Can you please help? at the end there is a sample output.
Thanks
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Account account = new Account();
            try
            {
                double overdraftLimit = 1000;
                double dailyWithdrawlLimit = 1000;
            }
            catch (OverdraftEx e1)
            {
                Console.WriteLine(e1.Message);
            }
            catch (DailyLimitEx e2)
            {
                Console.WriteLine(e2.Message);
            }
            account.display();
            account.deposit(1500);
            account.withdraw(1050);
            account.withdraw(1000);
            Console.WriteLine("-------------------");
            account.withdraw(50);
            account.endDay();
            account.withdraw(50);
            Console.ReadLine();
            return;
        }
    }

    class OverdraftEx : Exception
    {
        private static string msg = " Sorry, the amount you are trying to withdraw exceeds your allowed overdraft limit.";
        public OverdraftEx() : base(msg)
        {
        }
    }

    class DailyLimitEx : Exception
    {
        private static string msg1 = " Sorry, you have exceeded your allowed daily withdrawal limit.";
        public DailyLimitEx() : base(msg1)
        {
        }
    }

    class Account
    {
        public int accountID { get; set; }
        public double amount;

        public double balance;
        public double Balance
        {
            get
            {
                return balance;
            }
            set
            {
                if (amount > overdraftLimit)
                {
                    OverdraftEx ex1 = new OverdraftEx();
                    throw ex1;
                }
            }
        }

        public double overdraftLimit { get; set; }
        public double dailyWithdrawlLimit
        {
            get
            {
                return dailyWithdrawlLimit;
            }
            set
            {
                if (amount > dailyWithdrawlLimit - 1000)
                {
                    DailyLimitEx ex2 = new DailyLimitEx();
                    throw ex2;
                }
            }
        }

        public Account()
        {
            accountID = 10;
            balance = 0;
            return;
        }

        public Account(int accID, double accBalance)
        {
            accountID = accID;
            balance = accBalance;
            return;
        }

        public void withdraw(double amount)
        {
            Console.WriteLine("");
            Console.WriteLine(" Attempting to withdraw $" + Math.Round(amount, 2));
            balance = balance - amount;
            Console.WriteLine(" Account #: " + accountID);
            Console.WriteLine(" Your new balance is $" + Math.Round(balance, 2));
        }

        public void deposit(double amount)
        {
            // calculating the balance after depositing money
            if (amount > 0)
            {
                Console.WriteLine(" Depositing $" + Math.Round(amount, 2));
                balance = balance + amount;
                Console.WriteLine(" Account #: " + accountID);
                Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
            }
            else
                Console.WriteLine(" Sorry, amount is invalid");
            Console.WriteLine("");
            return;
        }

        public void display()
        {
            Console.WriteLine("");
            Console.WriteLine(" Account ID: " + accountID);
            Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
            Console.WriteLine("");
            return;
        }

        public void endDay()
        {
            Console.WriteLine("");
            Console.WriteLine(" End Day");
            Console.WriteLine(" Account ID: " + accountID);
            Console.WriteLine(" Balance: $" + Math.Round(balance, 2));
            return;
        }
    }
}
Sample output:
Note: the first error message should display: "Sorry, the amount you are trying to withdraw exceeds your allowed overdraft limit" not what is displayed below.

511
 
Last edited by a moderator:
A catch block will only catch exceptions thrown by code in the corresponding try block. In your code, the only part that is inside the try block is two declarations, which can't possibly throw an exception. If the parts that you think might throw an exception are the calls to withdraw then those calls need to be inside a try block, whether that be one for all of them, one each or something in between.

Apart from that, there are numerous issues with your Account class. I really suggest that you go right back to the drawing board with that class. For one thing, neither of your property setters actually set the property. You should start with the most barebones class definition that you can, i.e. just the properties, and make sure that you can get and set them properly. You should then add methods one by one, making sure that each does as it is supposed to before going on to the next. Only when all that is working properly should you try to add the exceptions. There's no way that you should be asking about throwing exceptions in a class that doesn't even do the basics. This is an example of one of the main reasons that beginners get into a tangle: they try to do many things at once and then don't know where to look for the problem when an issue arises. If you do one thing at a time, you know exactly where the problem is.
 
For one thing, your property setters are not using value. value is the value assigned to the property so, if you don't assign that to the backing field, the property won't be set. Here's an example of what a validated property might look like:
C#:
private decimal _amount;

public decimal Amount
{
    get => _amount;
    set
    {
        if (value > MaxAmount)
        {
            throw new ArgumentOutOfRangeException("'Amount' cannot be greater than 'MaxAmount'");
        }

        _amount = value;
    }
}
Note that value is not declared anywhere there. It is an implicit parameter in a property setter.
 
Back
Top Bottom