URLConnection.SendSync throws unexpected Exceptions

Unlike HTTPSocket, URLConnection will actually raise a runtime exception in some cases (for example, if the network is offline).

This can lead to your app dying with an unhandled exception, rather than failing more gracefully.

Here’s a workaround that I’m using - in a custom URLConnection subclass…

try
  me.SendSync "GET", mUrl, mFile
catch e as RuntimeException
  ' some exceptions we must not catch
  if e isa EndException or e isa ThreadEndException then
    raise e
    return
  end if
  
  ' properly route the error - do a callback on a timer
  Xojo.Timer.CallLater 1, addressOf ErrorCallback, e
end try



Event Error(msg as string)


Protected Sub ErrorCallback(a as Auto)
   dim e as RuntimeException = a
   ' raise the Error event with the message
    Error(e.Reason)
end sub