0

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 getCreditCardNumber() may throw an InvalidLengthException, a NonNumericException or an InvalidCardNumberException. Modify the method to do the following:
a. Catch the InvalidLengthException and print the message “Card number must be 16 digits.”
b. Catch the NonNumericException and print the message “Card number must be numbers only.”
c. Pass the InvalidCardNumberException 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 throw InvalidCardNumberException.

public void getOrderInformation() 
{
    getCreditCardNumber();
}
1
  • 2
    The instructions are already pretty clear. Is there any part in particular that doesn't make sense to you? Commented May 11, 2011 at 2:39

4 Answers 4

4

Here's the official documentation on exceptions. It's pretty short and the things you're trying to do are laid out in there.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

-1

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.");
    }
}

2 Comments

OP asked for help, not an answer, and surely not inaccurate answer.
If he didn't want the answer, he'd have left out the specifics :P
-1

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.”);
  }
}

1 Comment

This is not the answer as there are three separate sections. This is what would happen if you combined it all; he must dissect it to understand why each part works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.