gpt4 book ai didi

java - 异常和可抛出的未被捕获

转载 作者:行者123 更新时间:2023-11-30 08:47:04 25 4
gpt4 key购买 nike

我正在运行 Selenium 测试,我不希望线程结束,所以我正在尽我所能。这是我的程序结构

我的主.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();
}

测试.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();
}
}
}
}

即使我在 Test.java 中捕获异常和 Throwable,我仍然不时在 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

最佳答案

查看堆栈跟踪,您可以看到异常是由 driver.quit() 在 finally 中抛出的。如果您想捕获这些,您必须再次尝试围绕对 quit() 的调用{}

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

关于java - 异常和可抛出的未被捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32647228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com