开发者

help with java problem

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 NonNumericExcep开发者_Go百科tion 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();
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜