2
Reply

Problem with user defined exception class

Bill Cappoli

Bill Cappoli

Dec 9 2008 12:54 AM
4k
I'm working on a homework problem that requires that I use inheritance to create a user defined exception class. I have read a few tutorials and the chapter in the book and everything looks like it is correct but I still get an error that says my exception is unhandled.  Below is my code for the user exception class and where it is called. The class is supposed to throw the user defined exception if the debit methods cause the account balance to go below zero.

using System;

    public class Account
    {
        // private double data variable that will hold account balance
        private decimal accountBalance;

        // constructor that accepts one value which represents the starting balance of the account
        public Account(decimal bal)
        {
            // takes balance passed to constructor and assigns it to accountBlanace variable
            this.accountBalance = bal;

        } // end one parameter Account constructor

        // constructor that accepts no values setting balance to zero
        public Account()
        {
            accountBalance = 0;
        }  // end no parameter Account constructor

        // method that takes amount deposit by user and adds it to the balance
        public virtual decimal Credit(decimal amount)
        {
            return this.accountBalance += amount;
        }  // end of Credit method

        // method that takes amount withdrawn by user and subtracts it from the balance
        public virtual decimal Debit(decimal amount)
        {
            decimal debitAmount;

            debitAmount = this.accountBalance - amount;

            if (debitAmount < 0)
               throw new NotEnoughMoneyException("Transaction not allowed as it would bring account below a balance of $0");
            else
            return debitAmount;

        } // end of Debit method

        // assessor method providing access to current account balance
        public virtual decimal getBalance()
        {
            return accountBalance;
        } // end getBalance method

  } // end Account class


using System;

    // checking account class to handle checking account transactions
    class CheckingAccount : Account
    {
        private decimal transactionFee;

        // 2 parameter constructor
        public CheckingAccount(decimal bal, decimal transFee) : base(bal)
        {
            transactionFee = transFee;

        }  // end 2 parameter constructor

        // 1 parameter constructor
        public CheckingAccount(decimal transFee)
        {
            transactionFee = transFee;

            
        } // end 1 parameter constructor

        // overides Account class credit method to account for transaction fee
        public override decimal Credit(decimal amount)
        {
                return base.Credit(amount - transactionFee);
            
        } // override override credit method

        // overides Account class debit method to account for transaction fee
        public override decimal Debit(decimal amount)
        {
            decimal checkingDebit;

            checkingDebit = base.Debit(amount + transactionFee);

            if (checkingDebit < 0)
                throw new NotEnoughMoneyException("Transaction not allowed as it would bring account below a balance of $0");
            else
                return checkingDebit;

        } // end override debit method

    } // end of checking account class



using System;

    class NotEnoughMoneyException : Exception
    {
        // default constructor
        public NotEnoughMoneyException()
            : base("Illegal operation for debit from account.")
        {

        }// end default constructor

        // constructor for customizing error message
        public NotEnoughMoneyException(string message)
            : base(message)
        {

        } // end one argument constructor

        // constructor for customizing the exception's error meesage and specifying the InnerException object
        public NotEnoughMoneyException(string message, Exception inner)
            : base(message, inner)
        {

        } // end two argument constructor
    
    
    } // end NotEnoughMoneyException


Thanks for the help

Answers (2)