Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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 使用特定的try-catch,错误覆盖_Java - Fatal编程技术网

Java 使用特定的try-catch,错误覆盖

Java 使用特定的try-catch,错误覆盖,java,Java,这是我的例外: public class MyException extends Exception { private String errorCode="Unknown_Exception"; public MyException(String message, String errorCode){ super(message); this.errorCode=errorCode; } public String getErrorCode(){ return t

这是我的例外:

public class MyException extends Exception {

private String errorCode="Unknown_Exception";

public MyException(String message, String errorCode){
    super(message);
    this.errorCode=errorCode;
}

public String getErrorCode(){
    return this.errorCode;
}
}
现在在下一个场景中,代码太长,无法粘贴到这里:
1我在Swing-in演示包中获得了一个演示课程
2在包计算中,我使用接收到的数据库字段中的少量数字进行了简单的操作
3个包内连接我得到了数据库连接

麻烦来了:
-在表示层中,我捕获所有错误,如下所示:

    try {
        //here is a method called updateCombo() wich throws: 
        //throw new MyException(e.getMessage(),"ERROR_UPDATING_COMBO_BOX");
    } catch (MyException ex) {
        try {
            //Here we process error code, if error is not defined, uses default errors.
            processCode(ex);
        } catch (MyException ex1) {
            Logger.getLogger(Presentacion.class.getName()).log(Level.SEVERE, null, ex1);
        }
    }
private void processCode(MyException e) throws MyException {
    switch (e.getErrorCode()) {
        case "ERROR_UPDATING_COMBO_BOX":
            lblErrorText.setText("Error updating combo.");
            throw e;
        case "ERROR_SELECTING_PRIMARY_KEY":
            lblErrorText.setText("Error selecting PK");
            throw e;
        case "ERROR_OPENING_CONNECTION":
            lblErrorText.setText("Error opening connection.");
            throw e;
        default:
            lblErrorText.setText("Excepcion not defined: "+ e.getMessage());
            e.printStackTrace();
    }
processCode是一个包含案例的简单列表,如下所示:

    try {
        //here is a method called updateCombo() wich throws: 
        //throw new MyException(e.getMessage(),"ERROR_UPDATING_COMBO_BOX");
    } catch (MyException ex) {
        try {
            //Here we process error code, if error is not defined, uses default errors.
            processCode(ex);
        } catch (MyException ex1) {
            Logger.getLogger(Presentacion.class.getName()).log(Level.SEVERE, null, ex1);
        }
    }
private void processCode(MyException e) throws MyException {
    switch (e.getErrorCode()) {
        case "ERROR_UPDATING_COMBO_BOX":
            lblErrorText.setText("Error updating combo.");
            throw e;
        case "ERROR_SELECTING_PRIMARY_KEY":
            lblErrorText.setText("Error selecting PK");
            throw e;
        case "ERROR_OPENING_CONNECTION":
            lblErrorText.setText("Error opening connection.");
            throw e;
        default:
            lblErrorText.setText("Excepcion not defined: "+ e.getMessage());
            e.printStackTrace();
    }
这是一种情况,连接在第三个包中失败,并导致以下情况:

throw new MyException(e.getMessage(),"ERROR_OPENING_CONNECTION");
正如我所说的,错误被抛出到方法头中的上层WITHOWS子句,这是第二个包。
由于连接失败,第二个软件包还会向演示文稿引发新的异常:

throw new MyException(e.getMessage(),"ERROR_SELECTING_PRIMARY_KEY");
表示方法也会引发此异常,因为第二层失败:

throw new MyException(e.getMessage(),"ERROR_UPDATING_COMBO_BOX");
主要问题是: 使用debug,我发现程序完成了它必须完成的任务。它到达连接层并成功执行此操作:

throw new MyException(e.getMessage(),"ERROR_OPENING_CONNECTION");
但是,在第二层计算中,如果连接失败,它会抛出一个新的异常:

throw new MyException(e.getMessage(),"ERROR_SELECTING_PRIMARY_KEY");
public MyException(String message, Throwable cause, String errorCode){
    super(message, cause);
    this.errorCode = errorCode;
}
这就是问题所在:

throw new
引发新异常会覆盖错误\u打开\u连接时出错\u选择\u主键。当它由于“抛出新内容”而进入演示文稿时,会覆盖错误,\u选择\u主键,\u更新\u组合框,\u,导致屏幕上显示的最终错误:

lblErrorText.setText("Error updating combo.");
一旦发现第一个错误,有没有办法返回到演示文稿而不被下一个错误覆盖?
也许我误解了这个概念,但我想抓住所有可能的错误,因为:
-若连接正常,但第二层中的方法失败,它将抛出错误\u选择\u主键。
-如果第二层(计算)没有问题,但演示中出现错误,则应导致错误\u更新\u组合框

您可以使用
e.getCause()
返回一个
Throwable
并检查此原因是否属于
MyException
。在这种情况下,您可以再次递归地检查
e.getCause()
,直到获得stacktrace中最深的错误代码并对此异常执行验证

下面是一个例子:

public MyException getDeepestException(MyException e) {
    Throwable t = e.getCause();
    if (t instanceof MyException) {
        return getDeepestException((MyException)t);
    }
    return e;
}
正如所指出的,为了使用这种方法,您需要向自定义异常添加一个额外的构造函数:

throw new MyException(e.getMessage(),"ERROR_SELECTING_PRIMARY_KEY");
public MyException(String message, Throwable cause, String errorCode){
    super(message, cause);
    this.errorCode = errorCode;
}
抛出异常时,请调用适当的构造函数:

try {
    //...
} catch (SomeException e) {
    throw new MyException(<a proper message should be here>, e, "ERROR_SELECTING_PRIMARY_KEY");
}
试试看{
//...
}捕获(某些例外){
抛出新的MyException(,e,“选择主键时出错”);
}

如果我理解正确,如果一个包捕获的异常恰好是MyException,则希望传递原始MyException,否则(如果异常是其他类型的异常),则希望创建新的MyException

在这种情况下,您应该有两个catch子句

try {
    // Whatever you do in the try clause
} catch ( MyException myEx ) {
    throw myEx;
} catch ( Exception e ) {
    throw new MyException(e.getMessage(),"ERROR_SELECTING_PRIMARY_KEY");
}

好的,这将进入MyException类,但我如何在演示文稿中使用它?这正是您希望在
processCode
方法中使用的异常。请注意,为了使其有用,必须更改MyException的构造函数以接受原因,并且必须调用相应的
super()
设置原因。@realpoint您能在评论中发布它吗?:P@LuiggiMendoza好的,完成了,但还没有工作,xD我必须在MyException类中更改任何内容吗?