Throw a custom exception in optional in Java8
up vote
1
down vote
favorite
I have this piece of code where I want to throw an Exception wether the resource is not found
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(com.tdk.web.exception.ResourceNotFoundException(“menu " +
addMenuAmount.getMenuId() + " not found "));
but I got a compilation Error:
com.tdk.web.exception cannot be resolved to a type
java java-8 optional
add a comment |
up vote
1
down vote
favorite
I have this piece of code where I want to throw an Exception wether the resource is not found
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(com.tdk.web.exception.ResourceNotFoundException(“menu " +
addMenuAmount.getMenuId() + " not found "));
but I got a compilation Error:
com.tdk.web.exception cannot be resolved to a type
java java-8 optional
4
Optional.orElseThrow
takes aSupplier
that supplies/creates the exception. You appear to be passing the exception directly.
– Slaw
Nov 11 at 14:55
3
orElseThrow
expects aSupplier
as its only parameter. You can use a lambda expression to return your exception which is to be thrown.Supplier
's only method accepts no argument and in this case expects you to return anException
or a subclass.() -> new YourException()
would suffice.
– MC Emperor
Nov 11 at 14:57
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have this piece of code where I want to throw an Exception wether the resource is not found
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(com.tdk.web.exception.ResourceNotFoundException(“menu " +
addMenuAmount.getMenuId() + " not found "));
but I got a compilation Error:
com.tdk.web.exception cannot be resolved to a type
java java-8 optional
I have this piece of code where I want to throw an Exception wether the resource is not found
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(com.tdk.web.exception.ResourceNotFoundException(“menu " +
addMenuAmount.getMenuId() + " not found "));
but I got a compilation Error:
com.tdk.web.exception cannot be resolved to a type
java java-8 optional
java java-8 optional
asked Nov 11 at 14:50
En NuNYet de Can CalÇadA
1,050859132
1,050859132
4
Optional.orElseThrow
takes aSupplier
that supplies/creates the exception. You appear to be passing the exception directly.
– Slaw
Nov 11 at 14:55
3
orElseThrow
expects aSupplier
as its only parameter. You can use a lambda expression to return your exception which is to be thrown.Supplier
's only method accepts no argument and in this case expects you to return anException
or a subclass.() -> new YourException()
would suffice.
– MC Emperor
Nov 11 at 14:57
add a comment |
4
Optional.orElseThrow
takes aSupplier
that supplies/creates the exception. You appear to be passing the exception directly.
– Slaw
Nov 11 at 14:55
3
orElseThrow
expects aSupplier
as its only parameter. You can use a lambda expression to return your exception which is to be thrown.Supplier
's only method accepts no argument and in this case expects you to return anException
or a subclass.() -> new YourException()
would suffice.
– MC Emperor
Nov 11 at 14:57
4
4
Optional.orElseThrow
takes a Supplier
that supplies/creates the exception. You appear to be passing the exception directly.– Slaw
Nov 11 at 14:55
Optional.orElseThrow
takes a Supplier
that supplies/creates the exception. You appear to be passing the exception directly.– Slaw
Nov 11 at 14:55
3
3
orElseThrow
expects a Supplier
as its only parameter. You can use a lambda expression to return your exception which is to be thrown. Supplier
's only method accepts no argument and in this case expects you to return an Exception
or a subclass. () -> new YourException()
would suffice.– MC Emperor
Nov 11 at 14:57
orElseThrow
expects a Supplier
as its only parameter. You can use a lambda expression to return your exception which is to be thrown. Supplier
's only method accepts no argument and in this case expects you to return an Exception
or a subclass. () -> new YourException()
would suffice.– MC Emperor
Nov 11 at 14:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Try this and make sure that com.tdk.web.exception.ResourceNotFoundException
is accesible
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(() -> new com.tdk.web.exception.ResourceNotFoundException("menu " +
addMenuAmount.getMenuId() + " not found "));
Note the lambda () ->
and quotes "menu "
.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Try this and make sure that com.tdk.web.exception.ResourceNotFoundException
is accesible
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(() -> new com.tdk.web.exception.ResourceNotFoundException("menu " +
addMenuAmount.getMenuId() + " not found "));
Note the lambda () ->
and quotes "menu "
.
add a comment |
up vote
4
down vote
accepted
Try this and make sure that com.tdk.web.exception.ResourceNotFoundException
is accesible
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(() -> new com.tdk.web.exception.ResourceNotFoundException("menu " +
addMenuAmount.getMenuId() + " not found "));
Note the lambda () ->
and quotes "menu "
.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Try this and make sure that com.tdk.web.exception.ResourceNotFoundException
is accesible
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(() -> new com.tdk.web.exception.ResourceNotFoundException("menu " +
addMenuAmount.getMenuId() + " not found "));
Note the lambda () ->
and quotes "menu "
.
Try this and make sure that com.tdk.web.exception.ResourceNotFoundException
is accesible
Menu menu = menuService.findById(addMenuAmount.getMenuId())
.orElseThrow(() -> new com.tdk.web.exception.ResourceNotFoundException("menu " +
addMenuAmount.getMenuId() + " not found "));
Note the lambda () ->
and quotes "menu "
.
answered Nov 11 at 14:52
naXa
12.9k884131
12.9k884131
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249875%2fthrow-a-custom-exception-in-optional-in-java8%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Optional.orElseThrow
takes aSupplier
that supplies/creates the exception. You appear to be passing the exception directly.– Slaw
Nov 11 at 14:55
3
orElseThrow
expects aSupplier
as its only parameter. You can use a lambda expression to return your exception which is to be thrown.Supplier
's only method accepts no argument and in this case expects you to return anException
or a subclass.() -> new YourException()
would suffice.– MC Emperor
Nov 11 at 14:57