如何在java中再次抛出IOException?

如何在java中再次抛出IOException?,java,exception,methods,ioexception,throw,Java,Exception,Methods,Ioexception,Throw,代码: 但当我试着简单地说: catch (IOException e) { LOGGER.error("IOException exception happened"); //now need throw again the same exception to be //catched in the upper method } Eclipse假定我将“抛出e”放在try-catch块中。但

代码:

但当我试着简单地说:

 catch (IOException e) {
        LOGGER.error("IOException exception happened");
        //now need throw again the same exception to be 
                       //catched in    the    upper method
    }
Eclipse假定我将“抛出e”放在try-catch块中。但这是胡说八道。 如何解决这个问题? 谢谢。

由于是一个,如果您希望传播该方法,则需要将其声明为抛出
IOException
。例如:

  catch (IOException e) {
        LOGGER.error("IOException exception happened");
        //now need throw again the same exception to be 
                       //catched in    the    upper method
               throw e;
    }
由于是一个,如果希望传播该方法,则需要将其声明为抛出
IOException
。例如:

  catch (IOException e) {
        LOGGER.error("IOException exception happened");
        //now need throw again the same exception to be 
                       //catched in    the    upper method
               throw e;
    }

第二个代码片段很好。请记住,您必须将您的方法声明为:

void myMethod() throws IOException {
    try {
        //code
    }
    catch(IOException e) {
        LOGGER.error("IOException exception happened");
        throw e;
    }
}

第二个代码片段很好。请记住,您必须将您的方法声明为:

void myMethod() throws IOException {
    try {
        //code
    }
    catch(IOException e) {
        LOGGER.error("IOException exception happened");
        throw e;
    }
}

尝试将
抛出IOException
添加到您的方法中,例如:

public void myMethod() throws IOException {
    ...
}

然后Eclipse不会要求第二个try-catch块。

尝试向方法添加
抛出IOException
,例如:

public void myMethod() throws IOException {
    ...
}

然后Eclipse不会要求第二个try-catch块。

如果需要IOException进行传播,就不要捕捉它。你说这是胡说八道是什么意思?我需要捕捉-因为首先需要在底部方法中指定,然后在上部方法中指定如果需要IOException进行传播,只是不要抓住它。你说这是胡说八道是什么意思?我需要抓住它——因为首先需要在底部方法中明确,然后在上部方法中使用公共