5

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.

Pardha.Saradhi
  • 468
  • 1
  • 10
  • 27
  • Why aren't you throwing `KException`? – Makoto Dec 06 '17 at 23:25
  • You can convert it only if it's extending MyException. – whatamidoingwithmylife Dec 06 '17 at 23:25
  • Just throw a new exception like done here https://stackoverflow.com/questions/11972765/rethrow-exception-in-java – jontro Dec 06 '17 at 23:26
  • KException is from an External library. I have various handlers and logic on MyException. – Pardha.Saradhi Dec 06 '17 at 23:27
  • 1
    ...and you can't extend `KException`? – Makoto Dec 06 '17 at 23:27
  • 1
    wrap your `return` statement in `try-catch` and throw whatever you need. – PM 77-1 Dec 06 '17 at 23:31
  • it already extends it. Updated the question – Pardha.Saradhi Dec 06 '17 at 23:31
  • Tried it. Wrapping return in try catch doesn't work as p->p.getOrders() throws the exception which should be handled first. – Pardha.Saradhi Dec 06 '17 at 23:37
  • catch(KException e){ throw new MyException(e.message, e.code);}? – RAZ_Muh_Taz Dec 06 '17 at 23:46
  • 2
    What does it mean to return an empty `Optional>`? 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
  • Possible duplicate of [Java 8 Lambda function that throws exception?](https://stackoverflow.com/questions/18198176/java-8-lambda-function-that-throws-exception) – Didier L Dec 07 '17 at 00:07
  • Also [Java 8: Mandatory checked exceptions handling in lambda expressions. Why mandatory, not optional?](https://stackoverflow.com/questions/14039995/java-8-mandatory-checked-exceptions-handling-in-lambda-expressions-why-mandato) – Didier L Dec 07 '17 at 00:09
  • `Optional` and lambdas are a poor fit here. – erickson Dec 07 '17 at 01:03
  • This is pretty much the same issue as with your other question from yesterday: [Handling exceptions while returning values by Optional flatmap](https://stackoverflow.com/questions/47661370/handling-exceptions-while-returning-values-by-optional-flatmap). The solutions are similar: catch KException in the lambda, re-throw as RuntimeException, catch that one outside the lamba, re-throw as MyException. – Malte Hartwig Dec 07 '17 at 09:51

2 Answers2

1

as suggested by some of the comments, you can trap the exception within the map operation and then perform any further logic you need to do:

return getConnection(authDTO).map(p -> {
        try {
            return p.getOrders();
        } catch (KException e) {
            // perform some other logic
        }
});
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • From what I'm reading of the OP's requirements, `Order::getOrders` is likely to throw an exception. Strange, but that has nothing to do with the optional or any resultant throw (because by the time you bother throwing from the call `getOrders`, *another* exception's already been thrown). – Makoto Dec 06 '17 at 23:26
-2

Cant you just do like this?

@Override
public Optional<List<Order>> getPendingOrders(AuthDTO authDTO) throws MyException {
    return connector.getConnection(authDTO)
            .map(p->p.getOrders())
            .orElseThrow(MyException::new));
            
}
ajain
  • 444
  • 4
  • 7
  • 1
    `orElseThrow` will execute if the `map` function returns empty, I don't think it will catch an exception thrown in `map`. [ref](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#orElseThrow-java.util.function.Supplier-) – Jahorse Jun 07 '23 at 12:15