Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重写Java中未经检查的异常类中的方法_Java_Methods_Numberformatexception_Overriding_Unchecked Exception - Fatal编程技术网

重写Java中未经检查的异常类中的方法

重写Java中未经检查的异常类中的方法,java,methods,numberformatexception,overriding,unchecked-exception,Java,Methods,Numberformatexception,Overriding,Unchecked Exception,我试图重写Java中NumberFormatException类中的getMessage()方法,这是一个未经检查的异常。由于某种原因,我无法覆盖它。我知道这一定很简单,但我不明白我可能会错过什么。有人能帮忙吗?这是我的密码: public class NumberFormatSample extends Throwable{ private static void getNumbers(Scanner sc) { System.out.println("Enter any two i

我试图重写Java中NumberFormatException类中的getMessage()方法,这是一个未经检查的异常。由于某种原因,我无法覆盖它。我知道这一定很简单,但我不明白我可能会错过什么。有人能帮忙吗?这是我的密码:

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();
    }
}
public class NumberFormatSample扩展可丢弃{
专用静态无效GetNumber(扫描仪sc){
System.out.println(“输入0-9之间的任意两个整数:”);
int a=sc.nextInt();
int b=sc.nextInt();
如果(a<0 | a>9 | b<0 | b>9)
抛出新的NumberFormatException();
}
@凌驾
公共字符串getMessage(){
return“其中一个输入数字不在指定范围内!”;
}
公共静态void main(字符串[]args){
试一试{
GetNumber(新扫描仪(System.in));
}
捕获(NumberFormatException ex){
例如getMessage();
}
}
}编辑(在您的评论之后)

似乎您正在寻找:

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());
        }
    }
}
公共类编号格式示例{
专用静态无效GetNumber(扫描仪sc){
System.out.println(“输入0-9之间的任意两个整数:”);
int a=sc.nextInt();
int b=sc.nextInt();
如果(a<0 | a>9 | b<0 | b>9)
抛出新的NumberFormatException(“其中一个输入数字不在指定范围内!”);
}
公共静态void main(字符串[]args){
试一试{
GetNumber(新扫描仪(System.in));
}
捕获(NumberFormatException ex){
System.err.println(例如getMessage());
}
}
}
编辑(在您的评论之后)

似乎您正在寻找:

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());
        }
    }
}
公共类编号格式示例{
专用静态无效GetNumber(扫描仪sc){
System.out.println(“输入0-9之间的任意两个整数:”);
int a=sc.nextInt();
int b=sc.nextInt();
如果(a<0 | a>9 | b<0 | b>9)
抛出新的NumberFormatException(“其中一个输入数字不在指定范围内!”);
}
公共静态void main(字符串[]args){
试一试{
GetNumber(新扫描仪(System.in));
}
捕获(NumberFormatException ex){
System.err.println(例如getMessage());
}
}
}

您不需要重写任何内容或创建可丢弃的
的任何子类


只需调用
抛出新的NumberFormatException(message)

您不需要重写任何内容或创建任何
Throwable
的子类


只需调用
抛出新的NumberFormatException(message)

,正如其他答案所指出的,您实际尝试的操作根本不需要覆盖

但是,如果确实需要重写
NumberFormatException
中的方法,则必须:

  • 扩展该类,而不是
    可丢弃的
    ,以及
  • 实例化类的实例,而不是
    NumberFormatException
例如:

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

通过更改现有类重写不起作用。它的工作原理是在现有类的基础上创建一个新类。。。并使用新类。

正如其他答案所指出的,您实际尝试执行的操作根本不需要重写

但是,如果确实需要重写
NumberFormatException
中的方法,则必须:

  • 扩展该类,而不是
    可丢弃的
    ,以及
  • 实例化类的实例,而不是
    NumberFormatException
例如:

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

通过更改现有类重写不起作用。它的工作原理是在现有类的基础上创建一个新类。。。使用新类。

实际上,规范提到我应该只抛出NumberFormatException对象。是的,我刚刚得到了。我正在编辑原始帖子,但你已经先编辑了你的。事实上,说明书提到我应该只抛出NumberFormatException对象。是的,我刚刚得到了。我正在编辑原始帖子,但你已经先编辑了你的。谢谢你的精彩解释。谢谢你的精彩解释。