Reputation: 57
error: unreported exception NotEnoughBalance; must be caught or declared to be thrown
error: unreported exception NegativeWithdraw; must be caught or declared to be thrown
Basically, I'm unsure how my exceptions are not reported as I throw them and create a new exception when the conditions are met. My problem mainly deals with the fact that I'm putting two catch exceptions in the same method, using only one exception yields no errors.
These are the try demo statements that share a method in my object class
try {
account.withdraw(passNegative);
}
catch(NegativeWithdraw e) {
System.out.println(e.getMessage());
}
different sections of code
try {
account.withdraw(1);
}
catch(NotEnoughBalance e) {
System.out.println(e.getMessage());
}
Here is where I define the output when the program catches both either exception:
public class NegativeWithdraw extends Exception {
// This constructor uses a generic error message.
public NegativeWithdraw() {
super("Error: Negative withdraw");
}
// This constructor specifies the bad starting balance in the error message.
public NegativeWithdraw(double amount) {
super("Error: Negative withdraw: " + amount);
}
}
different programs
public class NotEnoughBalance extends Exception {
// This constructor uses a generic error message.
public NotEnoughBalance() {
super("Error: You don't have enough money in your bank account to withdraw that much");
}
// This constructor specifies the bad starting balance in the error message.
public NotEnoughBalance(double amount) {
super("Error: You don't have enough money in your bank account to withdraw $" + amount + ".");
}
}
And here is my object class, which compiles fine, but I think is where my program is located. I looked online to find how to hold multiple exceptions in one method, and found t hat you use a common between throwing exceptions, but am still a bit confused as to what I'm doing wrong.
public class BankAccount {
private double balance; // Account balance
// This constructor sets the starting balance at 0.0.
public BankAccount() {
balance = 0.0;
}
// The withdraw method withdraws an amount from the account.
public void withdraw(double amount) throws NegativeWithdraw, NotEnoughBalance {
if (amount < 0)
throw new NegativeWithdraw(amount);
else if (amount > balance)
throw new NotEnoughBalance(amount);
balance -= amount;
}
//set and get methods (not that important to code, but may be required to run)
public void setBalance(String str) {
balance = Double.parseDouble(str);
}
// The getBalance method returns the account balance.
public double getBalance() {
return balance;
}
}
Upvotes: 0
Views: 3623
Reputation: 358
Everytime you call the account.withdraw function, you need to catch both exceptions, because you don't know which one will be thrown (you might know, but the compiler doesn't)
For example
try {
account.withdraw(passNegative);
}
catch(NegativeWithdraw | NotEnoughBalance e) {
System.out.println(e.getMessage());
}
EDIT: As noted by another user, this is for Java 7
For older versions, you can do it the long way
try {
account.withdraw(passNegative);
} catch(NegativeWithdraw e) {
System.out.println(e.getMessage());
} catch(NotEnoughBalance e) {
System.out.println(e.getMessage());
}
Upvotes: 3