重写方法不会抛出异常 [英] overridden method does not throw exception

查看:2044
本文介绍了重写方法不会抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译我的代码时,我有一个问题,我试图使一个类的方法抛出一个个性化的异常,给定一些条件。但是在编译的时候,我收到消息:


重写方法不抛出异常




这是类和例外声明:

  public class UNGraph implements Graph 

是一个界面,所有方法 UNGraph (方法 getId()没有 throws

在构造函数之后我创建异常(在UNGraph类内):

  public class NoSuchElementException extends Exception {
public NoSuchElementException(String message){
super(message);
}
}

以下是异常的方法

  public int getId(....)throws NoSuchElementException {
if(condition be met){
// Do方法
返回变量;
}
else {
throw new NoSuchElementException(message);
}
}

显然我不希望该方法抛出只有当条件不满足时才会出现异常;当它满足时,我想返回一个变量。

解决方案

编译器发出错误是因为Java不允许覆盖一个方法并添加一个检查的Exception(任何用户定义的自定义异常,扩展 Exception 类)。因为很清楚,你想处理一些情况不是的情况,因为意外的发生(一个错误),你最好的选择是抛出一个 RuntimeException 。一个 RuntimeException ,例如: IllegalArgumentException NullPointerException ,不需要包含在方法签名中,所以您将减轻编译器错误。



我建议您的代码进行以下更改:

  //首先:将基类异常更改为RuntimeException:
public class NoSuchElementException extends RuntimeException {
public NoSuchElementException(String message){
超(消息);
}
}

//第二:删除getId签名的异常子句
//(并删除不必要的其他结构):
public int getId(....){
if(condition is met){return variable; }
//如果不满足条件,异常将被抛出:
throw new NoSuchElementException(message);
}


I have a problem when compiling my code, I'm trying to make a method of a class throw an personalized exception, given some conditions. But at the time of compiling I get the message:

Overridden method does not throw exception

Here's the class and exception declaration:

public class UNGraph implements Graph

Graph is an interface with all the methods of UNGraph in it (the method getId() doesn't have the throws declaration on that script)

After the constructor I create the exception (inside the class UNGraph):

public class NoSuchElementException extends Exception {
    public NoSuchElementException(String message){
        super(message);
    }
}

Here is the method with the exception

public int getId(....) throws NoSuchElementException {
    if (condition is met) {
        //Do method
        return variable;
    }
    else{
       throw new NoSuchElementException (message);
    }
}

Obviously I don't want the method to throw an exception every time, just when the condition is not met; and when it's met, I want to return a variable.

解决方案

The compiler is issuing an error because Java does not allow you to override a method and add a checked Exception (any user-defined custom exception that extends the Exception class). Because it is clear that you want to handle the scenario where some condition is not met as an unexpected occurrence (a bug), your best option is to throw a RuntimeException. A RuntimeException, such as: IllegalArgumentException or NullPointerException, does not have to be included in a method signature, so you will alleviate your compiler error.

I suggest the following changes to your code:

//First: Change the base class exception to RuntimeException:
public class NoSuchElementException extends RuntimeException {
    public NoSuchElementException(String message){
        super(message);
    }
}

//Second: Remove the exception clause of the getId signature
//(and remove the unnecessary else structure):
public int getId(....) {
    if ( condition is met) { return variable; }
    //Exception will only be thrown if condition is not met:
    throw new NoSuchElementException (message);
}

这篇关于重写方法不会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆