gpt4 book ai didi

java - 代理 "throw exception"

转载 作者:行者123 更新时间:2023-12-02 07:42:48 32 4
gpt4 key购买 nike

假设我有下面的代码,需要在不修改函数的情况下从匿名函数中抛出异常:

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
}
});
}

最佳答案

我有些怀疑我是否正确理解了这一点,但这是我的观点。我猜您正在尝试以某种方式从匿名类中移动异常并从父方法中抛出它:

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;
}

关于java - 代理 "throw exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11360312/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com