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?
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.
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With