title: Flaw in checking HTTP 500 error HttpUrlConnection Java
body: <p>My issue is with <code>status != 200</code> , when I run this script (3) different times, it'll print out the value for <code>if{}</code> (1) times, the <code>else{}</code> (1) time, and the <code>catch{}</code> another.</p>
  
  <p>I am simply trying to just print out in the <code>if(status != 200 || showTitleAttr == null || showTitleAttr == "")</code> the system.out.println(); message if the HTTP Error is not an HTTP 200 Error code. 
  The logic makes sense, but it is still not working all the time and falling into the <code>catch{}</code> block. I'm starting to think it is a problem with the variable <code>status</code> instead. </p>
  
  <p>Thank you.</p>
  
  <p><strong>Small update</strong>
  even if I try: <code>conn.getResponseCode() != HttpURLConnection.HTTP_OK</code> it seems to not change which block it falls into.. (1) time each for <code>if{}</code>, <code>else{}</code>, <code>catch{}</code></p>
  
  <p>The error I receive when it hits the <code>catch{}</code> block is:</p>
  
  <pre><code>java.io.IOException: Server returned HTTP response code: 500 for URL
  </code></pre>
  
  <p><strong>Q:</strong> Is there a better way to be checking for an HTTP error code that is anything than an HTTP 200 Error?</p>
  
  <p><strong>The code:</strong></p>
  
  <pre><code> public static void main(String args[]) {
          HttpException HttpExc = new HttpException();
          try {
                  // setup Show Title Parser
                  DOMParser parser = new DOMParser();
  
                  // Set the Url to Parse and assign to object
                  String UrlToParse = "urlishere.com";
                      URL obj = new URL(UrlToParse);
  
                  // Try opening a http connection the the url above
                  HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
                  int status = conn.getResponseCode();    
  
                  // Setup parser
                  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() );
                  NodeList nodes = IndivdualProgram.getChildNodes();
                  //String execType = getNodeAttr("programTitle", exec);
  
                  // Assign the Show Title to the NodeValue from traversing
                  String showTitleAttr = getNodeValue("programTitle", nodes);
  
                  // check to if the fetched Show Title isn't: 1) Returned bad error code 2) is null or 3) is empty 
                      // if it is... display the default value for title and exit.
                      // otherwise, print the Program Title.
  
                  if(status != 200 || showTitleAttr == null || showTitleAttr == ""){
                      System.out.println("You’re watching XX Plus");
                      System.exit(0);
                  }
  
                  else{
                      System.out.println("Program title is: " + showTitleAttr);
                  }
  
          }
              catch(HttpException e){
                  e.getStackTrace();
              }
  
              catch (Exception e) {
                  //e.printStackTrace();
                  System.out.println("You’re watching XX Plus");
                  System.exit(0);
              }
       }
  }
  </code></pre>