Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw custom exception in Optional

The orignal code is

if(object==null){
  throw new CustomException(ErrorEnum.OBJECT_NULL)
}

now I want to use Optional to handle NullPointerException. Just like this

Optional.ofNullable(object).orElseThrow(()->{throw new CustomException(ErrorEnum.OBJECT_NULL);}

But doing this makes code much longer than original. Maybe I should use the first way to solve the problem?

like image 645
StupidPz Avatar asked Mar 27 '18 10:03

StupidPz


People also ask

How do you throw an exception in optional?

Simply put, if the value is present, then isPresent() would return true, and calling get() will return this value. Otherwise, it throws NoSuchElementException. There's also a method orElseThrow(Supplier<? extends X> exceptionSupplier) that allows us to provide a custom Exception instance.

What exception is thrown by optional get () when not nested within optional isPresent ()?

NoSuchElementException Exception Via orElseThrow() Since Java 10. Using the Optional. orElseThrow() method represents another elegant alternative to the isPresent()-get() pair. Sometimes, when an Optional value is not present, all you want to do is to throw a java.

Can we Throws custom exception?

You can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions. All exceptions must be a child of Throwable. If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.

What does Optional orElseThrow do?

The orElseThrow method will return the value present in the Optional object. If the value is not present, then the supplier function passed as an argument is executed and an exception created on the function is thrown.


2 Answers

Optionals are intended as a safer alternative than a reference of type T that refers to an object or null, but it's only safer or better if you use it correctly. Both the examples given are safe but it's not better to use an Optional to solely replace an if statement as shown.

Also, note that you're expected to supply the desired exception, not to throw it. i.e.

orElseThrow(() ->{throw new CustomException(ErrorEnum.OBJECT_NULL);}

should be

orElseThrow(() ->{return new CustomException(ErrorEnum.OBJECT_NULL);} 

or better

orElseThrow(() -> new CustomException(ErrorEnum.OBJECT_NULL));

That said, in this particular, I'd go with the first approach. optionals are useful when they're used correctly, i.e as a method return type to wrap a value that may or may not be present as well as code documentation. Thus, in this case, there is no need to use it.

like image 115
Ousmane D. Avatar answered Sep 27 '22 21:09

Ousmane D.


I agree with commenters and other answers who are saying that Optional is unnecessary here. Indeed, it echoes advice I've also given elsewhere. In this case if one already has a nullable variable object then it's rather roundabout to wrap it in an Optional just to throw an exception, and as you observe, it's longer than the original.

The question to ask instead is, where did the reference in object come from? Did it come from another method that returns a nullable reference? If so, then perhaps that other method should be refactored to return Optional<T> instead of a nullable reference of type T.

like image 40
Stuart Marks Avatar answered Sep 27 '22 20:09

Stuart Marks