java 重新抛出 InvocationTargetException 目标异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10214525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:04:46  来源:igfitidea点击:

Re-throw an InvocationTargetException target exception

javaexception-handlinginvocationtargetexception

提问by Vincent Catalano

How does one re-throw the target exception of an InvocationTargetException. I have a method which uses reflection to call the invoke() method within one of my classes. However, if there is an Exception thrown within my code, I am not concerned about the InvocationTargetException and only want the target exception. Here is an example:

如何重新抛出 InvocationTargetException 的目标异常。我有一个方法,它使用反射在我的一个类中调用 invoke() 方法。但是,如果在我的代码中抛出异常,我不关心 InvocationTargetException 并且只想要目标异常。下面是一个例子:

public static Object executeViewComponent(String name, Component c,
        HttpServletRequest request) throws Exception {

    try {
        return c.getClass()
                .getMethod(c.getMetaData().getMethod(), HttpServletRequest.class)
                .invoke(c, request);
    } catch (InvocationTargetException e) {
        // throw the target exception here
    }
}

The primary problem I am facing is that calling throw e.getCause(); doesn't throw an Exception but rather throws a Throwable. Perhaps I am approaching this incorrectly?

我面临的主要问题是调用 throw e.getCause(); 不会抛出异常,而是抛出一个 Throwable。也许我不正确地接近这个?

回答by JB Nizet

catch (InvocationTargetException e) {
    if (e.getCause() instanceof Exception) {
        throw (Exception) e.getCause();
    }
    else {
        // decide what you want to do. The cause is probably an error, or it's null.
    }
}

回答by Tim Bender

Exception#getCausereturns a Throwable. If you want the compiler to think you are throwing an Exceptionthen you probably need to cast it.

Exception#getCause返回一个Throwable. 如果您希望编译器认为您正在抛出一个,Exception那么您可能需要对其进行转换。

throw (Exception) e.getCause();

回答by emory

The below is verbose, but I like to avoid reflection and casting. I don't think (but am not sure) that Java 7's multi catch syntax would be useful.

下面是冗长的,但我喜欢避免反射和投射。我不认为(但不确定)Java 7 的多捕获语法会有用。

public static Object executeViewComponent(String name, Component c,
        HttpServletRequest request) throw KnownException_1 , KnownException_2 , ... , KnownException_n {

    try {
        return c.getClass()
                .getMethod(c.getMetaData().getMethod(), HttpServletRequest.class)
                .invoke(c, request);
    }
    catch ( InvocationTargetException cause )
    {
          assert cause . getCause ( ) != null : "Null Cause" ;
          try
          {
               throw cause . getCause ( ) ;
          }
          catch ( KnownException_1 c )
          {
                throw c
          }
          catch ( KnownException_2 c )
          {
                throw c
          }
          ...
          catch ( KnownException_n c )
          {
                throw c
          }
          catch ( RuntimeException c )
          {
                throw c ;
          }
          catch ( Error c )
          {
                throw c ;
          }
          catch ( Throwable c )
          {
                assert false : "Unknown Cause" ;
          }
    }
}

回答by Stephan

You can rethrow any exception you caught before by using the throw keyword and the corresponding object you caught:

您可以使用 throw 关键字和您捕获的相应对象重新抛出您之前捕获的任何异常:

catch (XXXException e)
{
       throw e;
}

回答by Peter Lawrey

You can rethrow the cause without declaring it explicitly.

您可以在不明确声明的情况下重新抛出原因。

public static Object executeViewComponent(String name, Component c,
        HttpServletRequest request) throw /* known exceptions */ {

    try {
        return c.getClass()
                .getMethod(c.getMetaData().getMethod(), HttpServletRequest.class)
                .invoke(c, request);
    } catch (InvocationTargetException e) {
        // rethrow any exception.
        Thread.currentThread().stop(e.getCause());
    }
}