this is a question for my school work i dont want the complete answer but how i should go about doing each part or help clarifying what the question is asking for
In the following method, the call to
a. Catch thegetCreditCardNumber()
may throw anInvalidLengthException
, aNonNumericException
or anInvalidCardNumberException
. Modify the method to do the following:InvalidLengthException
and print the message “Card number must be 16 digits.” b. Catch theNonNumericExcep开发者_Go百科tion
and print the message “Card number must be numbers only.” c. Pass theInvalidCardNumberException
on to the calling method. In other words, don’t catch it, but let any calling method that uses this method know that it may throwInvalidCardNumberException
.
public void getOrderInformation()
{
getCreditCardNumber();
}
Here's the official documentation on exceptions. It's pretty short and the things you're trying to do are laid out in there.
Without providing exact code, per your request, you'll need to wrap your call to getCreditCardNumber() in a try
/catch
block using mutliple catch
statements.
This how Java, and other languages, perform exception handling. Read this quick tutorial and give it a shot.
You mean this? o.0
public void getOrderInformation() throws InvalidCardNumberException
{
try
{
getCreditCardNumber();
}
catch(InvalidLengthException e)
{
System.out.println("Card number must be 16 digits.");
}
catch(NonNumericException e)
{
System.out.println("Card number must be numbers only.");
}
}
Here is the answer for all of these together; you can take it apart to see what I did.
public void getOrderInformation() throws InvalidCardNumberException
{
try {
getCreditCardNumber();
} catch(InvalidLengthException ex) {
System.err.println("Card number must be 16 digits.");
} catch(NonNumericException ex) {
System.err.println(“Card number must be numbers only.”);
}
}
精彩评论