Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Continue after Server returned HTTP response code: 500 for URL

 
Mark Nicholas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably a more low level error handle question:
The following code is used to get the contents of multiple url's, however when the server returns a 500 response code the program exits:
So my question is how do you contine / handle the 500 exception more gracefully ?
java.io.IOException: Server returned HTTP response code: 500 for URL: http://www.some.url
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:691)
at java.net.URL.openStream(URL.java:955)
at com.jm.utils.UrlReader.<init>(UrlReader.java:36)
at com.jm.prj.webgrab.ex01.WebGrabEX01.grab(WebGrabEX01.java:42)
at com.jm.prj.webgrab.ex01.WebGrabEX01.main(WebGrabEX01.java:29)

[ October 08, 2002: Message edited by: Mark Nicholas ]
[ October 08, 2002: Message edited by: Mark Nicholas ]
[ October 08, 2002: Message edited by: Mark Nicholas ]
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is ungraceful about the way it works? When an error occurs, an exception is thrown. This is entirely as it should be In your case, you can catch the exception within the hosts loop, log it, and continue with the next iteration (and a fresh URL object).
- Peter
 
Mark Nicholas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it hangs because when the server throws a 500 the 'is' stream variable is null and I forgot to wrap the is.close() with a checking if is != null. Otherwise your right on the money.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic