Page 1 of 1

URL Errors - 404 Unknown & 500 Timeout

PostPosted: Sat Mar 03, 2012 5:21 pm
by jhb50
I'm a newbie with groovy and java.

When getting a URL
  Code:
            String videohtml = new URL(videoUrl).getText()

I can first test to ensure it exists and avoid 404 errors using this code
  Code:
   Boolean URLExists(URL fileURL){
      if(((HttpURLConnection) fileURL.openConnection()).getResponseCode() == 404){
         return false
      }
      return true
   }


But how do I avoid or catch 500 errors like this
  Code:
Caught: java.net.ConnectException: Connection timed out: connect
java.net.ConnectException: Connection timed out: connect
        at HaHaSport.extractItems(HaHaSport.groovytest.txt:190)
        at HaHaSport$extractItems.call(Unknown Source)
        at HaHaSport.main(HaHaSport.groovytest.txt:1320)


I'd like to catch them and retry rather than get an exception.

Re: URL Errors - 404 Unknown & 500 Timeout

PostPosted: Sat Mar 03, 2012 9:15 pm
by zip
you can just wrap the code in try - catch block (http://groovy.codehaus.org/JN3035-Exceptions) and catch the timeout exception.

Not sure if re-try os the way to go though, depends if there is a chance it'll work the second time around, otherwise it'll take ages to add the items