gpt4 book ai didi

java - 如何在一个 try/catch block 中捕获异常和 SocketTimeOut 异常

转载 作者:行者123 更新时间:2023-12-01 07:52:35 24 4
gpt4 key购买 nike

我已经为 URLconnection 和 Parser 类实现了一个 try catch block ,如下所示。

try {
Url uri = new Url(urlString);
Parser parse = new Parser(uri);
} catch (Exception e)
{
//ignore some other exceptions
}
catch (SocketTimeOutException e)
{
//I want to catch this exception and do some thing or restart
//if it's a timeout issue.
//I am using a proxy for the network connection at JVM setting
//using setProperty
}

所以,我的问题是如何根据 SocketTimeOutException 情况采取相应的行动,而对于其他异常则忽略。

谢谢,

最佳答案

正如java规范所说( http://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html#jls-11.2.3http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20 ),首先匹配,首先执行。

简单地反转你的 catch 子句:

try {
Url uri = new Url(urlString);
Parser parse = new Parser(uri);
} catch (SocketTimeOutException e) {
//I want to cache this ecption and do some thing or restart based
//if its timeout issue
//am using proxy for the network connection at JVM setting
//using setProperty
} catch (Exception e) {
//ingnore some other excpetions
}

关于java - 如何在一个 try/catch block 中捕获异常和 SocketTimeOut 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34906149/

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