简体   繁体   中英

Override a method from an unchecked Exception class in Java

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

}

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

Just call throw new NumberFormatException(message) .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM