Hi Rose.
An exception is not an error. The Exception and Error classes are subclasses of the Throwable class. You probably got the following problem when you tried to compile:
|
Quote:
|
/C:/Users/User/Desktop/CreditCard.java:73: exception NotEnoughMoneyException is never thrown in body of
corresponding try statement
}catch (NotEnoughMoneyException e){
^
/C:/Users/User/Desktop/CreditCard.java:75: exception overTheLimitException is never thrown in body of
corresponding try statement
}catch(overTheLimitException e){
^
2 errors
|
The compiler said that no exceptions are being thrown in your block of code. So you need to throw them according to your solution. So, let's say, the balance is negative when the person is in dept, and the limit is always positive. Then you could do this:
|
Code:
|
public void chargeCard(double amount){
try {
if(balance - amount >= 0 - limit){
// it's okay to make more dept
balance-=amount;
} else {
// the limit is reached
throw new overTheLimitException();
}
} catch (overTheLimitException e) {
System.err.println("Caught overTheLimitException: "+ e.toString());
}
} |
This example shows you how to throw an exception and how to recognize when no exceptions are being thrown, and you are trying to catch them.
Good luck Rose.