0

I am trying to override the getMessage() method in the NumberFormatException class in Java, which is an unchecked Exception. For some reason, I am unable to override it. I know it must be something really simple, but can't understand what I could be missing. Could someone please help? Here is my code:

public class NumberFormatSample extends Throwable{

private static void getNumbers(Scanner sc) {
    System.out.println("Enter any two integers between 0-9 : ");
    int a = sc.nextInt();
    int b = sc.nextInt();
    if(a < 0 || a > 9 || b < 0 || b > 9)
        throw new NumberFormatException();
}

@Override
public String getMessage() {
    return "One of the input numbers was not within the specified range!";

}
public static void main(String[] args) {
    try {
        getNumbers(new Scanner(System.in));
    }
    catch(NumberFormatException ex) {
        ex.getMessage();
    }
}

}

Setafire
  • 719
  • 2
  • 9
  • 21

3 Answers3

3

You don't need to override anything or create any subclasses of Throwable.

Just call throw new NumberFormatException(message).

Lone nebula
  • 4,768
  • 2
  • 16
  • 16
1

EDIT (after your comment).

Seems you are looking for:

public class NumberFormatSample {

    private static void getNumbers(Scanner sc) {
        System.out.println("Enter any two integers between 0-9 : ");
        int a = sc.nextInt();
        int b = sc.nextInt();
        if(a < 0 || a > 9 || b < 0 || b > 9)
            throw new NumberFormatException("One of the input numbers was not within the specified range!");
    }

    public static void main(String[] args) {
        try {
            getNumbers(new Scanner(System.in));
        }
        catch(NumberFormatException ex) {
            System.err.println(ex.getMessage());
        }
    }
}
jlordo
  • 37,490
  • 6
  • 58
  • 83
1

As other answers point out, what you are actually trying to do does not require an override at all.

However, if you really do need to override a method in NumberFormatException, you must:

  • extend that class, not Throwable, and
  • instantiate an instance of your class, not NumberFormatException.

For example:

// (Note: this is not a solution - it is an illustration!)
public class MyNumberFormatException extends NumberFormatException {

    private static void getNumbers(Scanner sc) {
        ...
        // Note: instantiate "my" class, not the standard one.  If you new
        // the standard one, you will get the standard 'getMessage()' behaviour.
        throw new MyNumberFormatException();
    }

    @Override
    public String getMessage() {
        return "One of the input numbers was not within the specified range!";
    }

    public static void main(String[] args) {
        try {
            getNumbers(new Scanner(System.in));
        }
        // Note: we can still catch NumberFormatException, because our
        // custom exception is a subclass of NumberFormatException.
        catch (NumberFormatException ex) {
            ex.getMessage();
        }
    }
}

Overriding does not work by changing an existing class. It works by creating a new class based on an existing one ... and using the new class.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216