【问题标题】:throws statement for handled exceptions -- Java处理异常的 throws 语句——Java
【发布时间】:2013-08-31 20:50:10
【问题描述】:

假设如下代码:

public static void somMethod() throws IOException {

try {
    // some code that can throw an IOException and no other checked exceptions
} catch (IOException e) {
        // some stuff here -- no exception thrown in this block
}

}

someMethod 抛出 IOException,并且没有其他检查异常, 并自行处理该异常。

究竟是什么

throws IOException 

在它的声明中是带进来的? 据我所知,它使这些方法成为可能 调用 someMethod() 自己处理 IOException

这里还有其他事情吗?

【问题讨论】:

  • 如果catch 块重新抛出异常,则throws IOException 是调用该方法的任何代码所必需的。
  • 不,捕获数据。很清楚。
  • ..但是谢谢。
  • 我希望我能回答这个问题,但我无法理解你的问题。您能否改写或添加更多细节?谢谢。
  • @Nayuki Minase 宁愿要求验证——而不是 Q 本身。看起来很简单,背后没有任何东西。

标签: java exception-handling throws checked-exceptions


【解决方案1】:

如果 catch 块没有抛出 IOException,则方法签名中的 throws IOException 部分是不必要的。而且,每次调用 someMethod() 时,都必须为实际上从未发生过的可能异常提供一个 catch 块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多