繁体   English   中英

尝试从另一种方法捕捉

[英]Try Catching from another Method

尝试从另一种方法捕捉:

method1(){
   try {

       method2();

   }catch(Exception e){


   }
}

 method2(){
    try{

       //ERROR FROM HERE

    }catch(Exception e){

    }

 }

method1()如何从method2()捕获错误?

method1()不会捕获错误,除非你从method2()catch块中重新抛出它。

void method2() {
    try {
        // Error here
    } catch(Exception e) {
        throw e;
    }
}

如果在method2的catch块中抛出另一个异常。

public void method2() {
    try {
        // ...
    } catch(Exception e) {
        throw new NullPointerException();
    }
}
    public void method1(){
        try {
            test2();
        } catch (IOException ex) {
            //catch test2() error
        }
    }

    public void method2() throws IOException{

    }

使用投掷

直到你通过添加throw e;将它重新抛出到method2catch块中throw e;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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