我正在学习RxJava,我在很多地方都看到错误可以通过这种方式处理:
repository.getById(10).subscribe(new Action1<User>() {
@Override
public void call(User user) {
//Do something
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable t) {
if (t instanceof FirstErrorException) {
handleFirstError((FirstErrorException) t);
} else if (t instanceof FirstErrorException) {
handleSecondError((SecondErrorException) t);
} else {
//and so on...
}
}
});
Run Code Online (Sandbox Code Playgroud)
我是唯一一个认为这是非常糟糕的代码吗?我怎样才能让它变得更好?我虽然使用Visitor模式"访问"我的基本异常的每个具体类型,但onError方法总是需要一个Action1<Throwable>; 你不能只使用你自己的基本例外Throwable.