Throw a custom exception in optional in Java8











up vote
1
down vote

favorite
1












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









share|improve this question


















  • 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








  • 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















up vote
1
down vote

favorite
1












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









share|improve this question


















  • 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








  • 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













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 14:50









En NuNYet de Can CalÇadA

1,050859132




1,050859132








  • 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








  • 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














  • 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








  • 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








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












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 ".






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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 ".






    share|improve this answer

























      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 ".






      share|improve this answer























        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 ".






        share|improve this answer












        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 ".







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 14:52









        naXa

        12.9k884131




        12.9k884131






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation