GWT 的 RPC 调用的 onFailure()
方法中是否可以抛出异常?因为这个方法稍后会在服务器响应后被调用,我很抱歉这里可能会发生一些不好的事情..
例如:
public void method() {
try {
rpc.invoke(new AsyncCallback<Void>() {
@Override
public void onSuccess(Void arg0) {}
@Override
public void onFailure(Throwable arg0) {
throw new RuntimeException("Error message"); //HERE
}
});
}
catch (Exception e) {
Window.alert(e.getMessage()); // AND CATCH ABOVE EXCEPTION HERE
}
}
最佳答案
我通常在我的 GWT 项目中使用以下方法:
1)创建 MyExceptionsHandler:
@Singleton
public class MyExceptionsHandler implements
GWT.UncaughtExceptionHandler,
RpcFailureEvent.Handler, // create corresponding GwtEvent-s
AnyOtherErrorEvent.Handler {
public MyExceptionsHandler(EventBus evenBus) {
eventBus.addHandler(RpcFailureEvent.TYPE, this);
eventBus.addHandler(AnyOtherErrorEvent.TYPE, this);
}
// implement corresponding methods for interfaces
}
2)在入口点:
GWT.setUnchaughtExceptionHandler(myExceptionHandler);
3)在任何其他地方,您都会遇到错误,但您不知道如何处理:
rpc.invoke(new AsyncCallback<Void>() {
@Override
public void onSuccess(Void arg0) {}
@Override
public void onFailure(Throwable arg0) {
eventBus.fireEvent(new RpcFailureEvent(<any context info you think helpful>));
}
});
关于java - 总重量。在 onFailure() 内抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291031/