user703141
user703141

Reputation: 49

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 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();
}

Upvotes: 0

Views: 112

Answers (4)

Suroot
Suroot

Reputation: 4423

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

Upvotes: -1

MicronXD
MicronXD

Reputation: 2220

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

Upvotes: -1

Jason McCreary
Jason McCreary

Reputation: 72971

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.

Upvotes: 1

Tom Chandler
Tom Chandler

Reputation: 642

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

Upvotes: 4

Related Questions