【发布时间】:2014-02-21 01:47:46
【问题描述】:
我处理一个用 GWT 设计的网站,我想检查在访问该网站之间互联网连接是否中断。如果互联网出现故障,我想发送消息,因为无法连接到服务器或 Gmail 之类的东西可以处理它。
任何人都可以建议处理此问题的最佳方法吗?
【问题讨论】:
标签: gwt
我处理一个用 GWT 设计的网站,我想检查在访问该网站之间互联网连接是否中断。如果互联网出现故障,我想发送消息,因为无法连接到服务器或 Gmail 之类的东西可以处理它。
任何人都可以建议处理此问题的最佳方法吗?
【问题讨论】:
标签: gwt
这就是AsyncCallback 上的onFailure(Throwable t) 方法的用途。当 RPC 因任何原因失败时调用此方法,包括(但不限于)连接丢失。
由于传递给onFailure() 的Throwable 可以是任何东西,因此文档中使用的模式是:
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
}
}
具体来说,缺少互联网连接会导致InvocationException 被传递给onFailure()
【讨论】:
caught然后抓住它而不是if (caught instanceof InvocationException) { ... } ...。您是否有指向您提到的文档的链接,该链接解释了为什么推荐这种模式?
else 语句,重新抛出它的好处是不会丢失异常。