Java Reference
In-Depth Information
"withdrawal of " + amount + " exceeds
balance of " + balance);
}
Now you need to define the InsufficientFundsException class. Should it be
a checked or an unchecked exception? Is it the fault of some external event, or is it
the fault of the programmer? We take the position that the programmer could have
prevented the exceptional conditionÈŒafter all, it would have been an easy matter to
check whether amount <= account.getBalance() before calling the
withdraw method. Therefore, the exception should be an unchecked exception and
extend the RuntimeException class or one of its subclasses.
You can design your own exception typesÈŒsubclasses of Exception or
RuntimeException .
It is customary to provide two constructors for an exception class: a default
constructor and a constructor that accepts a message string describing the reason for
the exception. Here is the definition of the exception class.
public class InsufficientFundsException
extends RuntimeException
{
public InsufficientFundsException() {}
public InsufficientFundsException(String message)
{
super(message);
}
}
When the exception is caught, its message string can be retrieved using the
getMessage method of the RunTimeException class.
S ELF C HECK
11. What is the purpose of the call super(message) in the second
InsufficientFundsException constructor?
12. Suppose you read bank account data from a file. Contrary to your
expectation, the next input value is not of type double . You decide to
Search WWH ::




Custom Search