Java IndexOutOfBoundsException 抛出的 UndeclaredThrowableException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19690653/
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-08-12 19:25:37  来源:igfitidea点击:

UndeclaredThrowableException thrown by IndexOutOfBoundsException

javaproxy-classes

提问by Nathan Merrill

I am using the decorator pattern for a List<WebElement>.Part of this decoration entails using a proxy.

我正在使用装饰器模式List<WebElement>。此装饰的一部分需要使用代理。

When I call get(index)with an index that is out of bounds, it throws an IndexOutOfBoundsexception, which is then caught by the proxy, and wrapped with an UndeclaredThrowableException.

当我get(index)使用越界的索引调用时,它会抛出一个IndexOutOfBounds异常,然后被代理捕获,并用UndeclaredThrowableException.

My understanding is that it should onlydo this if its a checked exception. IndexOutOfBoundsis an uncheckedexception, so why is it getting wrapped?

我的理解是,只有在检查异常时才应该这样做。 IndexOutOfBounds是一个未经检查的异常,那么为什么它会被包装?

It still gets wrapped even if I add throws IndexOutOfBoundsto my invokefunction.

即使我添加throws IndexOutOfBounds到我的invoke函数中,它仍然会被包裹。

Here's my code:

这是我的代码:

@SuppressWarnings("unchecked")
public WebElementList findWebElementList(final By by){
    return new WebElementList(
            (List<WebElement>) Proxy.newProxyInstance(this.getClass().getClassLoader(),
                    new Class<?>[] { List.class }, new InvocationHandler() {
        // Lazy initialized instance of WebElement
        private List<WebElement> webElements;

        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            if (webElements == null) {
                webElements = findElements(by);
            }
            return method.invoke(webElements, args);
        }
    }), driver);
}

Here's part of my stacktrace:

这是我的堆栈跟踪的一部分:

java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy30.get(Unknown Source)
at org.lds.ldsp.enhancements.WebElementList.get(WebElementList.java:29)
...
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
... 41 more

采纳答案by Zorg

Vic Jang is right. You need to wrap the invocation in try-catch and rethrow the inner exception.

张维克是对的。您需要将调用包装在 try-catch 中并重新抛出内部异常。

try {
  return method.invoke(webElements, args);
} catch (InvocationTargetException ite) {
  throw ite.getCause();
}

The reason is that "Method.invoke" wraps in InvocationTargetException those exceptions which are thrown in the method's code.

原因是“Method.invoke”将在方法代码中抛出的异常包装在 InvocationTargetException 中。

java.lang.reflect.Method:

java.lang.reflect.Method:

Throws:
...
InvocationTargetException - if the underlying method throws an exception.

抛出:
...
InvocationTargetException - 如果底层方法抛出异常。

java.lang.reflect.InvocationTargetException:

java.lang.reflect.InvocationTargetException:

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

InvocationTargetException 是一个经过检查的异常,它包装了被调用的方法或构造函数抛出的异常。

The proxy object's class doesn't have InvocationTargetException declared among its "throws". That leads to UndeclaredThrowableException.

代理对象的类没有在其“抛出”中声明 InvocationTargetException。这会导致 UndeclaredThrowableException。

回答by Vic Jang

I don't have enough reputation to comment. Your problem seems very similar to this one

我没有足够的声誉发表评论。您的问题似乎与非常相似

You can look at the solution there and see if that works.

您可以查看那里的解决方案,看看是否有效。

In short, you need to wrap your method.invokeinto try{}, catchthe exception, and throwit again.

简而言之,您需要将您的 包装method.invoketry{}catch异常,然后throw再次包装。