gpt4 book ai didi

java - UnknownHostException java

转载 作者:行者123 更新时间:2023-12-05 07:54:29 26 4
gpt4 key购买 nike

我的程序在一段时间内完美运行,但之后出现错误

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:666)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.ReviewsSearch.getJsonResponse(ReviewsSearch.java:28)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:110)

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1167)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1103)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.productHierarchySearch.getHierarchy(productHierarchySearch.java:27)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:115)

任何想法,可能是什么原因?让我知道是否需要更多信息

编辑:我尝试了 ping 命令,我意识到这是互联网连接。我在 ssh 服务器上运行该程序,想知道如何防止代码停止。我希望它等待连接。

最佳答案

其他答案解决了您看到的特定异常。但是,您(可能)真的在问该怎么办。

正如您所遇到的,在现实世界中,事情有时会失败(并抛出异常)。在预计异常是暂时的情况下,您应该捕获异常并安排重试该操作。您可能想立即重试,但更多时候最好稍等片刻再重试。这个想法是让发生的任何问题在重试之前得到修复(或自行修复)。

记录异常(或至少是摘要)通常是个好主意,以防可能需要运算符(operator)干预才能让事情重新开始。

如果在该操作成功之前无法继续,您可能希望在循环中重试。如果您可以处理其他事情,则可以通过将问题项目放在工作队列的后面来实现重试。

对于前一种情况:

doWork("www.sears.com");

成为

while (true) {
try {
doWork("www.sears.com");
break;
} catch (UnknownHostException e) {
logger.log(e.getMessage());
Thread.sleep(10000);
}
}

关于java - UnknownHostException java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31252915/

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