在Java App中处理Server 500错误时出错

时间:2013-06-19 13:26:56

标签: java error-handling try-catch http-error

我正在努力不只是使用catch(Exception e) {e.printStackTrace();}并让应用程序的用户更清楚地了解我的应用程序失败的原因。

因此,我想检测是否抛出了给定的服务器错误,例如Http 500错误。

目前,我的代码正在抛出堆栈跟踪:

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错误?

谢谢

0 个答案:

没有答案