我在服务器端有这个方法,通过 RPC 与客户端代码进行通信。
@Override
public void registerStudent(param1, param2...) throws IllegalArgumentException {
//some code here
try {
//some code here
} catch (ConstraintErrorViolationException e) {
throw new RegisterFailedException();
}
}
我有这段代码处理失败。
@Override
public void onFailure(Throwable caught) {
displayErrorBox("Could not register user", caught.getMessage());
}
目前,onFailure()
函数不区分随机异常和我希望处理和处理的特定异常,即 RegisterFailedException
。
如何成功地正确处理这两种不同类型的错误?
最佳答案
所以你的异常(exception)
public class RegisterFailedException extends RuntimeException {
public RegisterFailedException () {
super();
}
}
并且您的方法会抛出异常,例如
throws new RegisterFailedException();
然后在onFailure()
中检查
if (caught instanceof RegisterFailedException){
}
关于java - 使用RPC时如何区分服务器错误和完整性约束异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17701003/