Zeeboot Zeeboot - 5 months ago 12
Java Question

I cannot get my user defined checked exception to work properly

I am required to create a user defined checked exception. Unfortunately when I try to throw it I get the error: unreported exception InsufficientFunds; must be caught or declared to be thrown.

I have identified the exception in the method signature, and then thrown it within the method where I want it to happen. However, I still cannot understand the mistake I am making.

Any input would be greatly appreciated.

Code examples:
main()

if (e.getSource() == deposit) {
accountType.deposit(money);
} else if (e.getSource() == withdraw) {
if (money % 20 == 0) {
accountType.withdraw(money);
} else {
JOptionPane.showMessageDialog(null, "Entry must be in $20 increments.");
}
}


In this example, I am receiving the unreported exception error at "accountType.deposit(money);" which runs the following method:

public void withdraw(Double money) throws InsufficientFunds {
if (count >= 4) {
this.money = (money + FEE);
} else {
this.money = money;
}
if (this.balance < this.money) {
throw new InsufficientFunds("You don't have enough money to do that!");
} else {
this.balance -= this.money;
count++;
}
}


Here is my user defined exception class

public class InsufficientFunds extends Exception {

public InsufficientFunds(String s) {
super(s);
}
}


Anyone that can put a set of eyes on this and provide me with some knowledge would be greatly appreciated. I am sure I have overlooked something simply.

Thank you,
Bryan

Answer

You are throwing a custom exception called InsufficientFunds in your withdraw() method, but you never catch that exception in the calling method. So one way to make the error go away is to place the call to withdraw() inside a try-catch block:

if (e.getSource() == deposit) {
    accountType.deposit(money);
} else if (e.getSource() == withdraw) {
    if (money % 20 == 0) {
        try {
            accountType.withdraw(money);
        }
        catch (InsufficientFunds ife) {
            System.out.println("No funds to withdraw.");
        }
        catch (Exception e) {
            System.out.println("Some other error occurred.");
        }
    } else {
        JOptionPane.showMessageDialog(null, "Entry must be in $20 increments.");
    }
}

If you look closely, you will see that I first catch an InsufficientFunds exception, followed by a general Exception. This is the pattern you usually want to follow, namely catching specific exceptions followed by more general ones, and finally catching Exception to make certain that you handle every use case which might go wrong.

The alternative to this would be to have the method which calls withdraw() to also throw an exception, i.e.

public static void main(String[] args) throws InsufficientFunds {
    if (e.getSource() == deposit) {
        accountType.deposit(money);
    } else if (e.getSource() == withdraw) {
        if (money % 20 == 0) {
            accountType.withdraw(money);
        } else {
            JOptionPane.showMessageDialog(null, "Entry must be in $20 increments.");
        }
    }
}
Comments