How to wrap checked exceptions but keep the original runtime exceptions in Java

后端 未结 7 2266
一向
一向 2020-12-15 16:36

I have some code that might throw both checked and runtime exceptions.

I\'d like to catch the checked exception and wrap it with a runtime exception. But if a Runtim

相关标签:
7条回答
  • 2020-12-15 17:16

    Guava's Throwables.propagate() does exactly this:

    try {
        // some code that can throw both checked and runtime exception
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    

    UPDATE: This method is now deprecated. See this page for a detailed explanation.

    0 讨论(0)
提交回复
热议问题