Is even possible add more method inside of
findbyId(id).orElseThrow(() -> new BusinessException("bug"));
and not just Exception ? so something like
.orElseThrow(() -> { //do something here new BusinessException("bug") });
or use ".or" method where I can put more methods, I don't need Exceptions. So if doesn't exist then //do this // and this
because with classic method you add Entity as a Optional,then check in condition if isPresent and then if not, you can do couple more thing in else{}
I am not sure whether I understand you correctly and assume you want to add additional logic to
orElseThrowfor the first part.Add additional logic to
orElseThroworElseThrowalways throws an exception and that exception is taken from the lambda.If you want to add functionality before throwing the exception, make sure to return it:
However, if you want to add logic that decides whether or not to throw an exception, you can use
orto map to an optional containing the value to return that is empty if you want to throw the exception. Then, useorElseThrow:no exception
In case you don't want to throw an exception but have some other logic, you can use
orElseGet.This method just executes a lambda expression if the
Optionalis empty and returns the result: