在行driver = new ChromeDriver();上获取“ InvocationTargetException”异常。

时间:2019-04-25 09:39:31

标签: java selenium-webdriver reflection selenium-chromedriver invocationtargetexception

我正在打开Chrome浏览器,并得到"InvocationTargetException"的证明。该代码几天前已正常运行。这是我的代码

System.setProperty("webdriver.chrome.driver","D:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();

"driver=new ChromeDriver();"行,我遇到了"InvocationTargetException"异常

1 个答案:

答案 0 :(得分:1)

InvocationTargetException

InvocationTargetException是已检查的异常,它包装了由调用的方法或构造函数引发的异常。通过反射调用方法是一种额外的抽象层次。反射层将所有异常包装在InvocationTargetException中。现在将在构造时提供并通过getTargetException()方法访问的“目标异常”称为原因,并且可以通过Throwable.getCause()方法以及上述“旧方法”进行访问。 “

解决方案

最好的方法是在InvocationTargetException解包原因以获取原始异常。

try {

        System.setProperty("webdriver.chrome.driver","D:\\Automation\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

} catch (InvocationTargetException e) {
        // the real cause
        e.getCause().printStackTrace();

} catch (Exception e) {
        // generic exception handling
        e.printStackTrace();
}

最佳做法

按照最佳做法,请遵循以下准则: