1

我试图不只是使用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 错误?

谢谢

4

0 回答 0