【问题标题】:Passing Exception to Higher Try/Catch in Nested Try/Catch [Nesting Required]在嵌套的 Try/Catch 中将异常传递给更高的 Try/Catch [需要嵌套]
【发布时间】:2021-02-16 02:43:59
【问题描述】:

我有以下场景

try
{
   doSomething1();
   doSomething2();
   / * some other statements that might cause cause exceptions */

}
catch(Exception e)
{
  // log
}

在 doSomething1() 内部还有一些嵌套的 try/catch。我想知道我们是否可以将 doSomething1() 内部的异常传递给最外层的 try/catch。

【问题讨论】:

    标签: java exception nested try-catch


    【解决方案1】:

    您可以通过在方法声明中添加throws 子句并将异常处理委托给方法调用者来指定doSomething1() 可以引发异常:

    void doSomething1() throws IOException {
    
    }
    

    Specifying the Exceptions Thrown by a Method

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      相关资源
      最近更新 更多