如何在Java中再次抛出IOException? [英] How to throw again IOException in java?

查看:271
本文介绍了如何在Java中再次抛出IOException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

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

但是当我尝试简单地:

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

Eclipse想让我在try catch块中放入"throw e".但这是胡说八道. 如何解决这个问题? 谢谢.

Eclipse supposes to me put "throw e" in try catch block. But this is nonsense. How fix this problem? Thanks.

推荐答案

IOException 已检查的异常,该方法需要声明为throwing IOException(如果要传播).例如:

Since IOException is a checked exception, that method needs to be declared as throwing IOException if you want it to propagate. For example:

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

这篇关于如何在Java中再次抛出IOException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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