When to use trigger_error() vs throw new Error()?










3














I throw new Exception when I can catch a failure and do something about it. If I can't do something about it then I just trigger_error().



Now there's something new to me in PHP 7: throw new Error.



E.g.,



if (!mail(...))
throw new Error('...');


or



if (!mail(...))
trigger_error('...');


If I don't want to catch the error or do something in case mail() fails should I use throw new Error() or just the plain old trigger_error()?



What instances should we use throw new Error() vs a simple trigger_error()?










share|improve this question


























    3














    I throw new Exception when I can catch a failure and do something about it. If I can't do something about it then I just trigger_error().



    Now there's something new to me in PHP 7: throw new Error.



    E.g.,



    if (!mail(...))
    throw new Error('...');


    or



    if (!mail(...))
    trigger_error('...');


    If I don't want to catch the error or do something in case mail() fails should I use throw new Error() or just the plain old trigger_error()?



    What instances should we use throw new Error() vs a simple trigger_error()?










    share|improve this question
























      3












      3








      3







      I throw new Exception when I can catch a failure and do something about it. If I can't do something about it then I just trigger_error().



      Now there's something new to me in PHP 7: throw new Error.



      E.g.,



      if (!mail(...))
      throw new Error('...');


      or



      if (!mail(...))
      trigger_error('...');


      If I don't want to catch the error or do something in case mail() fails should I use throw new Error() or just the plain old trigger_error()?



      What instances should we use throw new Error() vs a simple trigger_error()?










      share|improve this question













      I throw new Exception when I can catch a failure and do something about it. If I can't do something about it then I just trigger_error().



      Now there's something new to me in PHP 7: throw new Error.



      E.g.,



      if (!mail(...))
      throw new Error('...');


      or



      if (!mail(...))
      trigger_error('...');


      If I don't want to catch the error or do something in case mail() fails should I use throw new Error() or just the plain old trigger_error()?



      What instances should we use throw new Error() vs a simple trigger_error()?







      php exception error-handling php-7






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 13:03









      IMB

      4,113124386




      4,113124386






















          1 Answer
          1






          active

          oldest

          votes


















          2














          The Error class was, as you already mentioned, introduced in PHP 7.



          It acts exactly like an exception since it implements Throwable.



          It's also subclasses by the following:



           ArithmeticError
          DivisionByZeroError
          AssertionError
          ParseError
          TypeError
          ArgumentCountError


          However, it does not behave like trigger_error(), in fact it behaves exactly like an exception(mostly because it is one).



          If you trigger_error() you can't catch it, because it's not an exception. Although there are workarounds.




          This is mostly guestimating on my part.



          I assume this exists for a more clear separation of Errors and Exceptions, possibly future plans include deprecating trigger_error and leaving just Error.




          To answer your question.



          I'd stick to throwing stuff rather than trigger_error. It's catchable, it's OOP.






          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',
            autoActivateHeartbeat: false,
            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%2f53262778%2fwhen-to-use-trigger-error-vs-throw-new-error%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









            2














            The Error class was, as you already mentioned, introduced in PHP 7.



            It acts exactly like an exception since it implements Throwable.



            It's also subclasses by the following:



             ArithmeticError
            DivisionByZeroError
            AssertionError
            ParseError
            TypeError
            ArgumentCountError


            However, it does not behave like trigger_error(), in fact it behaves exactly like an exception(mostly because it is one).



            If you trigger_error() you can't catch it, because it's not an exception. Although there are workarounds.




            This is mostly guestimating on my part.



            I assume this exists for a more clear separation of Errors and Exceptions, possibly future plans include deprecating trigger_error and leaving just Error.




            To answer your question.



            I'd stick to throwing stuff rather than trigger_error. It's catchable, it's OOP.






            share|improve this answer

























              2














              The Error class was, as you already mentioned, introduced in PHP 7.



              It acts exactly like an exception since it implements Throwable.



              It's also subclasses by the following:



               ArithmeticError
              DivisionByZeroError
              AssertionError
              ParseError
              TypeError
              ArgumentCountError


              However, it does not behave like trigger_error(), in fact it behaves exactly like an exception(mostly because it is one).



              If you trigger_error() you can't catch it, because it's not an exception. Although there are workarounds.




              This is mostly guestimating on my part.



              I assume this exists for a more clear separation of Errors and Exceptions, possibly future plans include deprecating trigger_error and leaving just Error.




              To answer your question.



              I'd stick to throwing stuff rather than trigger_error. It's catchable, it's OOP.






              share|improve this answer























                2












                2








                2






                The Error class was, as you already mentioned, introduced in PHP 7.



                It acts exactly like an exception since it implements Throwable.



                It's also subclasses by the following:



                 ArithmeticError
                DivisionByZeroError
                AssertionError
                ParseError
                TypeError
                ArgumentCountError


                However, it does not behave like trigger_error(), in fact it behaves exactly like an exception(mostly because it is one).



                If you trigger_error() you can't catch it, because it's not an exception. Although there are workarounds.




                This is mostly guestimating on my part.



                I assume this exists for a more clear separation of Errors and Exceptions, possibly future plans include deprecating trigger_error and leaving just Error.




                To answer your question.



                I'd stick to throwing stuff rather than trigger_error. It's catchable, it's OOP.






                share|improve this answer












                The Error class was, as you already mentioned, introduced in PHP 7.



                It acts exactly like an exception since it implements Throwable.



                It's also subclasses by the following:



                 ArithmeticError
                DivisionByZeroError
                AssertionError
                ParseError
                TypeError
                ArgumentCountError


                However, it does not behave like trigger_error(), in fact it behaves exactly like an exception(mostly because it is one).



                If you trigger_error() you can't catch it, because it's not an exception. Although there are workarounds.




                This is mostly guestimating on my part.



                I assume this exists for a more clear separation of Errors and Exceptions, possibly future plans include deprecating trigger_error and leaving just Error.




                To answer your question.



                I'd stick to throwing stuff rather than trigger_error. It's catchable, it's OOP.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 13:14









                Andrei

                1,97931223




                1,97931223



























                    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%2f53262778%2fwhen-to-use-trigger-error-vs-throw-new-error%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







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3