3

I handle a website which is designed in GWT and I want to check if internet connection goes down in between accessing the website. If internet is down I want to give message as cannot connect to server or something like Gmail handles it.

Can anybody suggest what will be the best way to handle this?

Priya
  • 451
  • 4
  • 8
  • 15

1 Answers1

2

This is what the onFailure(Throwable t) method on AsyncCallback is for. This method is called when the RPC fails for any reason, including (but not limited to) loss of connection.

Since the Throwable that gets passed into onFailure() can be anything, the pattern used in the docs is:

public void onFailure(Throwable caught) {
  // Convenient way to find out which exception was thrown.
  try {
    throw caught;
  } catch (IncompatibleRemoteServiceException e) {
    // this client is not compatible with the server; cleanup and refresh the 
    // browser
  } catch (InvocationException e) {
    // the call didn't complete cleanly
  // other Throwables may be caught here...
  } catch (Throwable e) {
    // last resort -- a very unexpected exception
  }
}

Specifically, a lack of internet connection will cause an InvocationException to be passed to onFailure()

Jason Hall
  • 20,632
  • 4
  • 50
  • 57
  • Why do you rethrow `caught` and then catch it instead of `if (caught instanceof InvocationException) { ... } ...`. Do you have a link to the docs that you mentioned which explains why this pattern is recommended? – Robert Petermeier Apr 10 '10 at 11:26
  • 1
    Either way will work, it's just a matter of preference. That's the the pattern they use in the example. Re-throwing it has the advantage of the exception not being lost if you forget to add an `else` statement. – Jason Hall Apr 10 '10 at 11:53
  • Thanks Jason for your reply. I just used if(caught instanceof InvocationException){..} and it works for me. Thanks a lot. – Priya Apr 10 '10 at 13:38