在Java应用程序中处理服务器500错误时出错
我试图不只是使用在Java应用程序中处理服务器500错误时出错,java,error-handling,try-catch,http-error,Java,Error Handling,Try Catch,Http Error,我试图不只是使用catch(Exception e){e.printStackTrace();},而是让应用程序的用户更清楚地了解我的应用程序可能失败的原因 因此,我想检测是否抛出了给定的服务器错误,例如HTTP500错误 目前,我的代码正在抛出一个堆栈跟踪: java.io.IOException: Server returned HTTP response code: 500 在下面的catch块中: public static void main(String args[]) {
catch(Exception e){e.printStackTrace();}
,而是让应用程序的用户更清楚地了解我的应用程序可能失败的原因
因此,我想检测是否抛出了给定的服务器错误,例如HTTP500错误
目前,我的代码正在抛出一个堆栈跟踪:
java.io.IOException: Server returned HTTP response code: 500
在下面的catch块中:
public static void main(String args[]) {
try {
DOMParser parser = new DOMParser();
String UrlToParse = "theurlishereinmycode.com";
parser.parse(UrlToParse);
Document doc = parser.getDocument();
// Get the document's root XML node
NodeList PGRoot = doc.getChildNodes();
// Navigate down the hierarchy to get to the program node
Node comp = getNode("ProgramGuideWCSResponse", PGRoot);
Node PG = getNode("programGuide", comp.getChildNodes() );
Node Programs = getNode("programs", PG.getChildNodes() );
Node IndivdualProgram = getNode("program", Programs.getChildNodes() );
//String execType = getNodeAttr("programTitle", exec);
// Load the executive's data from the XML
NodeList nodes = IndivdualProgram.getChildNodes();
String showTitleAttr = getNodeValue("programTitle", nodes);
// check to make sure the fetched Show Title isn't: 1) null or 2) blank
// if it is... display the default value for title.
// else, print the Program Title.
if(showTitleAttr == null || showTitleAttr == ""){
System.out.println("You’re watching XX Plus");
}
else{
System.out.println("Program title is: " + showTitleAttr);
}
}
catch ( Exception e ) {
e.printStackTrace();
}
}
如何记录内部服务器错误的HTTP 500错误
谢谢您在如何显示错误消息或如何找出它是HTTP 500方面有困难吗?如何找出它是否是HTTP 500,就像在检测catch Block中是否抛出500错误时一样我猜这行代码是
Document doc=parser.getDocument()代码>引发异常。也许您应该调查(在getDocument中)是否有比IOException更具体的异常被抛出。那你就可以抓住那种类型了。一些HttpClient抛出“WebException”或类似的东西,例如。。。