IN JAVA
Problem 1
Write a custom exception class named ‘AccountNotFoundException’ that extends Exception and uses invokes the parent’s constructor passing the message “Account “ + accountNumber “ + “ does not exist!”.
Problem 2
Write a method called ‘verifyAccountStatus’ that takes the account number as a formal parameter. Assume there is logic in the method to call off to a database to determine if the account exists. Such a call may look like:
boolean accountExists = CHECKINGACCOUNTDAO.verifyAccountExists(accountNumber);
If the account does not exist, throw the custom exception written above and declare the method throws this exception in the method signature.
Problem 3
After declaring that the method throws the ‘AccountNotFoundException’, what happens? Would it make more sense to handle this exception in the veriftyAccountStatus method or have the caller deal with the exception?
Answer 1)
====
public class AccountNotFoundException extends Exception {
public AccountNotFoundException(int accountNumber )
{
super("Account " + accountNumber +
" does not exist!");
}
}
Answer 2)
=====
public boolean verifyAccountStatus(int accountNumber) throws
AccountNotFoundException {
boolean accountExists =
CHECKINGACCOUNTDAO.verifyAccountExists(accountNumber);
if(!accountExists)
throw new
AccountNotFoundException(accountNumber);
return accountExists;
}
Answer 3)
=====
The exception should be handled by the caller and not the
verifyAccountStatus() because the signature says it will throw the
exception.
Get Answers For Free
Most questions answered within 1 hours.