Java中异常捕获的一个问题
我有一个实例方法,在其中,我做了一个简单的网页解析:Java中异常捕获的一个问题,java,Java,我有一个实例方法,在其中,我做了一个简单的网页解析: public void doOperation(final AuthorAccess authorAccess, ArgumentsMap arguments) throws IllegalArgumentException,AuthorOperationException { final String server = "chiexist1.worldbook.com"; final String port = "808
public void doOperation(final AuthorAccess authorAccess, ArgumentsMap arguments) throws IllegalArgumentException,AuthorOperationException
{
final String server = "chiexist1.worldbook.com";
final String port = "8080";
try {
docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.parse("http://" + server + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
...}
catch{}
}
因为目前我正在字符串中硬编码服务器地址,所以可能存在服务器名称不正确的情况,因此在这种情况下,我希望它自动将服务器URL字符串更改为“localhost”
我认为if-else语句可能会起作用,但我不太确定如何确定用于检测此解析是否失败的布尔变量。我也考虑把它放在catch语句中,但是其他语句也抛出异常呢
我还检查了DocumentBuilder的API,parse()方法始终返回文档类型,但不返回布尔值。因此,如果有人能在这里给我一些建议,告诉我如何检测错误的URL,然后改为解析localhost,我将不胜感激。我想在这里你可以找到一个答案:一般提示:不要只写“catch”。始终指定要捕获的异常类型(最好是您自己的自定义异常类型),以便您知道代码完全按照您的意思执行。否则,正如您所说,catch语句可能会捕获一些您不想发生的其他类型的异常,这些异常实际上应该向上抛出。基本上,在catch中,只需从localhost构建另一个文档
final String server = "chiexist1.worldbook.com";
final String port = "8080";
docBuilder = docFactory.newDocumentBuilder();
doc = null;
try {
doc = docBuilder.parse("http://" + server + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
} catch{
try{
doc = docBuilder.parse("http://" + "localhost" + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
} catch {
// now we have an error we can't recover from
}
}
... // I meant to do this before.
跟踪变量和循环中的尝试次数,直到传递成功或最大尝试次数(我选择5):
String server = "chiexist1.worldbook.com";
final String port = "8080";
int attempts = 0;
final int MAX_ATTEMPTS = 5;
boolean success = false;
docBuilder = docFactory.newDocumentBuilder();
while (!success && attempts < MAX_ATTEMPTS) {
try {
doc = docBuilder.parse("http://" + server + ":" + port + "/exist/webdav/db/portfolio/config/products.xml");
success = true;
} catch (IOException e) {
if (++attempts < MAX_ATTEMPTS) {
System.err.println("Attempt to connect to " + server + ":" + port + " failed. Retrying" + (attempts == 1 ? ", but connecting to localhost:" + port + " this time" : "") + ".");
server = "localhost";
} else {
System.err.println("Attempt to connect to " + server + ":" + port + " failed. Giving up after " + attempts + " failed attempts.");
}
}
}
if (!success) {
// Tidy up and exit
}
// Do stuff
String server=“chiexist1.worldbook.com”;
最终字符串端口=“8080”;
int=0;
最终int最大尝试次数=5次;
布尔成功=假;
docBuilder=docFactory.newDocumentBuilder();
而(!成功和尝试次数<最大尝试次数){
试一试{
doc=docBuilder.parse(“http://“+server+”:“+port+”/exist/webdav/db/portfolio/config/products.xml”);
成功=真实;
}捕获(IOE异常){
如果(++尝试次数<最大尝试次数){
System.err.println(“尝试连接到“+服务器+”:“+端口+”失败。重试“+(尝试次数==1?”,但这次连接到本地主机:“+端口+”:)+”;
server=“localhost”;
}否则{
System.err.println(“尝试连接到“+服务器+”:“+端口+”失败。在“+尝试+”失败尝试后放弃”);
}
}
}
如果(!成功){
//清理并退出
}
//做事
您应该分别捕获各自的异常,并根据捕获的异常执行不同的错误处理逻辑。这被认为是最好的方法,因为它将错误条件从正常逻辑中分离出来,并且可以对不同的错误进行不同的处理
在您的正常逻辑流程中,它被认为比if-else语句更好。尝试以下方法:
static String hostname = "hostname";
static{
if(doesHostExist(hostName)==false){
hostname="localhost";
}
}
private static boolean doesHostExist(String host){
try {
URL url = new URL("http://" + host);
URLConnection conn = url.openConnection();
conn.connect();
return true;
} catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}
*编辑从上一个答案中提取的其他代码:如果URL无效,我猜它会返回IOException,但引发IOException的原因可能还有很多。顺便问一下,当你说“服务器名称不对”时,你能更准确地解释一下你的意思吗?@Gregs这基本上意味着我正在访问一些其他公司的服务器,而我没有访问权限,所以我被屏蔽了。但是,本地主机将始终工作。感谢您提供的信息。顺便说一下,我看到我写的是“返回IOException”而不是“抛出IOException”。我将很快向鞭笞室报告。@Gregs:我之前以为应该是IOException,但实际上,在我的代码中,只有SAXException可以正确捕获异常。最好在try中只包装“doc=docBuilder.parse(…)”语句。这样就避免了从进一步的语句中捕获异常。如果可以缩小例外的范围,那就更好了。@Marti,是的,那是我的错。我忘了移动
。
@marc,哎呀,是的。我不是故意的。@jjnguy,问题是,我需要保留最外面的试块。我能把它们放在窝里吗?甚至