这通常不是关于异常处理的问题,但它特别适用于某些框架的使用.典型起点的几个例子:
public void onFailure(Throwable caught)实现AsyncCallback.public Response toResponse(E throwable)实现ExceptionMapper<E extends Throwable>.上述两种方法都接收一个实例Throwable.通常,我看到开发人员使用一个简单的"if/else if"块来区分处理逻辑:
// As specified by the AsyncCallback class of the GWT framework
public void onFailure(Throwable caught) {
if (caught instanceof AnException) {
// handle AnException
} else if (caught instanceof AnotherException) {
// handle AnotherException
} else if (caught instanceof YetAnotherException) {
// handle YetAnotherException
} else if (caught instanceof ...) {
// and so on...
}
} …Run Code Online (Sandbox Code Playgroud)