tags:

views:

42

answers:

1

Hi All,

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?

A: 

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
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
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
Thanks Jason for your reply. I just used if(caught instanceof InvocationException){..} and it works for me. Thanks a lot.
Priya