假设我有以下代码需要在匿名函数中抛出异常而不修改函数:
FOO.doSomething(new Transactable(){
public void run(FOO foo) {
// How to proxy a exception throw
// from here, without modifying the class
}
});
像:
@Override
public void run() throws MyCustomException{
FOO.doSomething(new Transactable(){
public void run(FOO foo) {
// How to proxy a exception throw
// from here, without modifying the class
}
});
}
答案 0 :(得分:2)
我怀疑我是否理解这一点,但这是我的镜头。我想你试图以某种方式从匿名类移动异常并从父方法中抛出它:
class ExceptionWrapper {
public Exception exception;
}
@Override
public void run() throws MyCustomException{
final ExceptionWrapper ew = new ExceptionWrapper();
FOO.doSomething(new Transactable(){
public void run(FOO foo) {
try {
...
} catch(MyCustomException ex) {
ew.exception = ex;
}
}
});
if(ew.exception != null) throw (MyCustomException)ew.exception;
}