简体   繁体   English

异常和 throwable 未被捕获

[英]Exception and throwable not being caught

I am running Selenium tests and I do not want the thread to end so I am catching everything I can.我正在运行 Selenium 测试,但我不希望线程结束,所以我正在尽我所能。 This is the structure of my program这是我的程序结构

My Main.java我的主程序

for (int i = 0; i < numberOfThreads; i++)
{
    System.out.println("Inside loop to create threads!");
    System.out.println("Number of threads to create: " + numberOfThreads);
    totalNumberOfThreads++;
    String threadName = "Thread" + totalNumberOfThreads;
    if (type.contains("auto"))
    {
        newThread = new Thread(new Test());
    }

    UncaughtExceptionHandler handler = new UncaughtExceptionHandler()
    {
        @Override
        public void uncaughtException(Thread t, Throwable ex)
        {
            FileOutputStream fos = null;
            try
            {
                fos = new FileOutputStream(new File("throwable.txt"), true);
            }
            catch (FileNotFoundException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            PrintStream ps = new PrintStream(fos);
            ex.printStackTrace(ps);
        }
    };

    newThread.setName(threadName);
    newThread.setUncaughtExceptionHandler(handler);
    newThread.start();
}

Test.java测试.java

public class Test extends Common implements Runnable
{
    @Override
    public void run()
    {
        WebDriver driver = null;
        while (true)
        {
            try
            {
                driver = new PhantomJSDriver(caps);
                // bunch of test code
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            catch (Throwable t)
            {
                t.printStackTrace();
            }
            finally
            {
                driver.quit();
            }
        }
    }
}

Even though I am catching Exceptions and Throwables in Test.java I still see the following exception in throwable.txt from time to time which means the error is not being catched in Test.java即使我在 Test.java 中捕获 Exceptions 和 Throwables 我仍然不时在 throwable.txt 中看到以下异常,这意味着错误没有在 Test.java 中被捕获

org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.1.5-x86_64-linode61', java.version: '1.8.0_60'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:589)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:618)
    at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:464)
    at myprogram.Test.run(Test.java:155)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.openqa.selenium.WebDriverException: The PhantomJS/GhostDriver server has unexpectedly died!
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.1.5-x86_64-linode61', java.version: '1.8.0_60'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.phantomjs.PhantomJSCommandExecutor.execute(PhantomJSCommandExecutor.java:88)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:568)
    ... 4 more
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:30041 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:143)
    at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:89)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:134)
    at org.openqa.selenium.phantomjs.PhantomJSCommandExecutor.execute(PhantomJSCommandExecutor.java:82)
    ... 5 more
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:134)
    ... 18 more

Checking the stacktrace, you can see that the exception is thrown by the driver.quit(), in the finally.检查堆栈跟踪,您可以看到异常是由 driver.quit() 在 finally 中抛出的。 If you want to catch thoses, you have to surround the call to quit() by another try{}如果你想抓住那些,你必须用另一个 try{} 包围对 quit() 的调用

at org.openqa.selenium.remote.RemoteWebDriver.quit(RemoteWebDriver.java:464)
at myprogram.Test.run(Test.java:155)

Your finally is still not surrounded by the try/catch.你的 finally 仍然没有被 try/catch 包围。

You need something like:你需要这样的东西:

  finally{
      try{
        driver.quit();
      }catch(Exception ex){
        //do something
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM