我有一个这样的方法。
@Override
public Optional<List<Order>> getPendingOrders(AuthDTO authDTO) throws MyException {
return connector.getConnection(authDTO).map(p->p.getOrders());
}
Run Code Online (Sandbox Code Playgroud)
这里
connector.getConnection(authDTO)
Run Code Online (Sandbox Code Playgroud)
返回 Connection 的可选值和
p->p.getOrders()
Run Code Online (Sandbox Code Playgroud)
抛出 KException 我无法更改它的形式
public class KException extends Throwable {
// variables
public String message;
public int code;
// constructor that sets the message
public KException(String message){
this.message = message;
}
// constructor that sets the message and code
public KException(String message, int code){
this.message = message;
this.code = code;
}
}
Run Code Online (Sandbox Code Playgroud)
这是 MyException 的结构
public class MyException extends KException {
public MyException(String message, int code) {
super(message, code);
}
}
Run Code Online (Sandbox Code Playgroud)
该代码未编译并出现以下错误
unreported exception com.something.exceptions.KException; must be caught or declared to be thrown
Run Code Online (Sandbox Code Playgroud)
我想将这个 KException 转换为 MyException。有没有一种优雅的方法来做到这一点?请帮忙。
正如一些评论所建议的,您可以捕获操作中的异常map,然后执行您需要执行的任何进一步逻辑:
return getConnection(authDTO).map(p -> {
try {
return p.getOrders();
} catch (KException e) {
// perform some other logic
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10166 次 |
| 最近记录: |