I have a method like this.
@Override
public Optional<List<Order>> getPendingOrders(AuthDTO authDTO) throws MyException {
return connector.getConnection(authDTO).map(p->p.getOrders());
}
Here
connector.getConnection(authDTO)
returns an Optional of Connection and
p->p.getOrders()
throws KException which I cannot change and is of the form
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;
}
}
and this the strucute of MyException
public class MyException extends KException {
public MyException(String message, int code) {
super(message, code);
}
}
The code is not compiling with the below error
unreported exception com.something.exceptions.KException; must be caught or declared to be thrown
I want to convert this KException to MyException. Is there are an elegant way to do this? PLease help.
>`? Would it really have a different meaning from an empty `List`? You should avoid wrapping collections into optionals (and vice versa), as this is quite cumbersome to use for the caller.
– Didier L Dec 07 '17 at 00:01