Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throw exception in optional in Java8

Tags:

java

java-8

There is a method get(sql) (I can not modify it). This method returns MyObjects and it has to be in try catch block because JqlParseException is possible there. My code is:

String sql = something; try{    MyObject object = get(sql); } catch(JqlParseException e){    e.printStackTrace(); } catch(RuntimeException e){    e.printStackTrace(); } 

I want to remove try catch and use Optional class, I tried:

MyObject object = Optional.ofNullable(get(sql)).orElseThrow(RuntimeException::new); 

but IDE force there try catch too. And for:

MyObject object = Optional.ofNullable(get(sql)).orElseThrow(JqlParseException::new)); 

is an error (in IDE) The type JqlParseException does not define JqlParseException() that is applicable. Is there any way to avoid try catch blocks and use optional?

like image 515
joe paulens Avatar asked Mar 24 '17 07:03

joe paulens


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.

Which of the following methods of Optional class can be used to throw an exception of your choice if an object is null?

static <T> Optional<T> ofNullable(T value) Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.

What is isPresent in Optional?

Optional Class | isPresent() function The isPresent() function in Optional Class is used to evaluate whether the value if assigned to variable is present or not. Syntax value.isPresent() Returns: It returns true if value is assigned otherwise false.


1 Answers

Optional is not intended for the purpose of dealing with exceptions, it was intended to deal with potential nulls without breaking the flow of your program. For example:

 myOptional.map(Integer::parseInt).orElseThrow(() -> new RuntimeException("No data!"); 

This will automatically skip the map step if the optional was empty and go right to the throw step -- a nice unbroken program flow.

When you write:

 myOptionalValue.orElseThrow(() -> new RuntimeException("Unavailable")); 

... what you are really saying is: Return my optional value, but throw an exception if it is not available.

What you seem to want is a way to create an optional (that instantly catches the exception) and will rethrow that exception when you try using the optional.

like image 86
john16384 Avatar answered Sep 27 '22 19:09

john16384