【问题标题】:How does the correct rethrow code look like for this example此示例的正确重新抛出代码如何
【发布时间】:2011-09-15 19:16:14
【问题描述】:

昨天我把这个article 发红了关于 Java 7 中新的异常处理。

在文章中,他们展示了一个在 Java 6 中不起作用的示例(第 4 号)。我只是复制了它。

public class ExampleExceptionRethrowInvalid {

public static void demoRethrow()throws IOException {
    try {

        // forcing an IOException here as an example,
        // normally some code could trigger this.
        throw new IOException("Error");
    }
    catch(Exception exception) {
        /*
         * Do some handling and then rethrow.
         */
        throw exception;
    }
}

public static void main( String[] args )
{
    try {
        demoRethrow();
    }
    catch(IOException exception) {
        System.err.println(exception.getMessage());
    }
}
}

就像文章中描述的那样,它不会编译,因为类型不匹配 -throw IOException- 和 -throw exception-。在 Java 7 中它会。所以我的问题是。

如何在 Java 6 中正确实现这种重新抛出异常?我不喜欢建议的实施示例五。我知道这是一个品味和问题的问题,你是否尝试处理未经检查的异常。那么我该怎么做才能获得 -throws IOException- 并保留堆栈跟踪?我是否应该只将 catch 更改为 IOException 并冒着无法全部捕获的风险?

我很好奇你的答案。

【问题讨论】:

  • 这闻起来像是一个拼写错误。我认为它应该在第 10 行 catch(IOException exception) {... 否则这没有任何意义。
  • @Bobby 但这样就可以了。我认为这不是一个错字。

标签: java exception-handling


【解决方案1】:

简单地捕捉IOException,像这样:

public static void demoRethrow()throws IOException {
    try {
        // forcing an IOException here as an example,
        // normally some code could trigger this.
        throw new IOException("Error");
    }
    catch(IOException exception) {
        /*
         * Do some handling and then rethrow.
         */
        throw exception;
    }
}

如果try 块内的代码可以抛出除IOException 之外的已检查异常,编译器会将其标记为错误,因此您不会“冒险[ing] not catch all”。

如果您也对未经检查的异常感兴趣,您还可以捕获并重新抛出 RuntimeException(您无需在 throws 子句中声明它)。

【讨论】:

  • 我认为您的答案与来自 user470365 的示例相结合,对于已检查和未检查的异常都可以做到最好。谢谢。
【解决方案2】:

分别捕获IOException 和其他所有内容:

public static void demoRethrow() throws IOException {
    try {
        throw new IOException("Error");
    }
    catch(IOException exception) {
        throw exception;
    }
    catch(Exception exception) {
        throw new IOException(exception);
}

【讨论】:

    【解决方案3】:

    catch(Exception ex) 捕获已检查和未检查 (RuntimeException) 的异常。 因此,为了使其功能等效,

    public static void demoRethrow() throws IOException {
    try {
        throw new IOException("Error");
    }
    catch(IOException exception) {
        throw exception;
    }
    catch(RuntimeException exception) {
        throw new IOException(exception);
    }
    

    足够了,编译器将检测到其他已检查的异常(有利于再次考虑它们是否应该真正做到这一点,或者之前应该有 bean delt)

    【讨论】:

      【解决方案4】:

      在不让编译器检查异常的情况下抛出以捕获通用异常并重新抛出的一种老套方法是使用 stop。

      public static void demoRethrow() throws IOException {
        try {
          throw new IOException("Error");
      
        } catch(Throwable t) {
          // handle exception
      
          // rethrow the exception without compiler checks.
          Thread.currentThread().stop(t);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多