When should you create your own exception type?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












I am working on a project where we are restructuring old C code into new c++.
And for error handling, we are using exceptions. We are creating different exception types for different modules. But I don't see it is worth. But I don't have any valid argument to prove my point. So if we would have written the standard library, you would get to see vector_exception, list_excetpion and so on.



So while thinking about it I realized this question, when should you create your own exception type and when should you stick to the exceptions already created in std library.



Also what could be the problems that we may face in the near future if we go by the above approach. If there is any such problematic approach that you have used, I would be happy to hear it and learn from it.










share|improve this question





















  • Derive from exception and create custom exceptions; that is good for debugging because it helps the understanding of the code. Also, it gives a better overall structure
    – Alberto Miola
    1 hour ago










  • Idea is to have control. You catch the exceptions that your code rises, and you do only catches for those that you expect. This tells people who read your code which exceptions can happen here. If you catch some generic exception, it can come from anywhere, from your code, from dependencies, who knows, not the guy who looks at your code. If your reasoning not to do them is "it takes a full minute of my time", then rethink your reasoning. It's perfectly fine to derive them without adding anything, any functionality or the like. Just deriving them to be able to distinct them.
    – Aziuth
    39 mins ago










  • @Aziuth Then why there is a logical division of errors in std. And why only one bad_alloc. Why not list_bad_alloc, vector_bad_alloc. That is more descriptive. Isn't it?
    – Amit Bhaira
    32 mins ago















up vote
7
down vote

favorite












I am working on a project where we are restructuring old C code into new c++.
And for error handling, we are using exceptions. We are creating different exception types for different modules. But I don't see it is worth. But I don't have any valid argument to prove my point. So if we would have written the standard library, you would get to see vector_exception, list_excetpion and so on.



So while thinking about it I realized this question, when should you create your own exception type and when should you stick to the exceptions already created in std library.



Also what could be the problems that we may face in the near future if we go by the above approach. If there is any such problematic approach that you have used, I would be happy to hear it and learn from it.










share|improve this question





















  • Derive from exception and create custom exceptions; that is good for debugging because it helps the understanding of the code. Also, it gives a better overall structure
    – Alberto Miola
    1 hour ago










  • Idea is to have control. You catch the exceptions that your code rises, and you do only catches for those that you expect. This tells people who read your code which exceptions can happen here. If you catch some generic exception, it can come from anywhere, from your code, from dependencies, who knows, not the guy who looks at your code. If your reasoning not to do them is "it takes a full minute of my time", then rethink your reasoning. It's perfectly fine to derive them without adding anything, any functionality or the like. Just deriving them to be able to distinct them.
    – Aziuth
    39 mins ago










  • @Aziuth Then why there is a logical division of errors in std. And why only one bad_alloc. Why not list_bad_alloc, vector_bad_alloc. That is more descriptive. Isn't it?
    – Amit Bhaira
    32 mins ago













up vote
7
down vote

favorite









up vote
7
down vote

favorite











I am working on a project where we are restructuring old C code into new c++.
And for error handling, we are using exceptions. We are creating different exception types for different modules. But I don't see it is worth. But I don't have any valid argument to prove my point. So if we would have written the standard library, you would get to see vector_exception, list_excetpion and so on.



So while thinking about it I realized this question, when should you create your own exception type and when should you stick to the exceptions already created in std library.



Also what could be the problems that we may face in the near future if we go by the above approach. If there is any such problematic approach that you have used, I would be happy to hear it and learn from it.










share|improve this question













I am working on a project where we are restructuring old C code into new c++.
And for error handling, we are using exceptions. We are creating different exception types for different modules. But I don't see it is worth. But I don't have any valid argument to prove my point. So if we would have written the standard library, you would get to see vector_exception, list_excetpion and so on.



So while thinking about it I realized this question, when should you create your own exception type and when should you stick to the exceptions already created in std library.



Also what could be the problems that we may face in the near future if we go by the above approach. If there is any such problematic approach that you have used, I would be happy to hear it and learn from it.







c++ exception exception-handling






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 1 hour ago









Amit Bhaira

73741025




73741025











  • Derive from exception and create custom exceptions; that is good for debugging because it helps the understanding of the code. Also, it gives a better overall structure
    – Alberto Miola
    1 hour ago










  • Idea is to have control. You catch the exceptions that your code rises, and you do only catches for those that you expect. This tells people who read your code which exceptions can happen here. If you catch some generic exception, it can come from anywhere, from your code, from dependencies, who knows, not the guy who looks at your code. If your reasoning not to do them is "it takes a full minute of my time", then rethink your reasoning. It's perfectly fine to derive them without adding anything, any functionality or the like. Just deriving them to be able to distinct them.
    – Aziuth
    39 mins ago










  • @Aziuth Then why there is a logical division of errors in std. And why only one bad_alloc. Why not list_bad_alloc, vector_bad_alloc. That is more descriptive. Isn't it?
    – Amit Bhaira
    32 mins ago

















  • Derive from exception and create custom exceptions; that is good for debugging because it helps the understanding of the code. Also, it gives a better overall structure
    – Alberto Miola
    1 hour ago










  • Idea is to have control. You catch the exceptions that your code rises, and you do only catches for those that you expect. This tells people who read your code which exceptions can happen here. If you catch some generic exception, it can come from anywhere, from your code, from dependencies, who knows, not the guy who looks at your code. If your reasoning not to do them is "it takes a full minute of my time", then rethink your reasoning. It's perfectly fine to derive them without adding anything, any functionality or the like. Just deriving them to be able to distinct them.
    – Aziuth
    39 mins ago










  • @Aziuth Then why there is a logical division of errors in std. And why only one bad_alloc. Why not list_bad_alloc, vector_bad_alloc. That is more descriptive. Isn't it?
    – Amit Bhaira
    32 mins ago
















Derive from exception and create custom exceptions; that is good for debugging because it helps the understanding of the code. Also, it gives a better overall structure
– Alberto Miola
1 hour ago




Derive from exception and create custom exceptions; that is good for debugging because it helps the understanding of the code. Also, it gives a better overall structure
– Alberto Miola
1 hour ago












Idea is to have control. You catch the exceptions that your code rises, and you do only catches for those that you expect. This tells people who read your code which exceptions can happen here. If you catch some generic exception, it can come from anywhere, from your code, from dependencies, who knows, not the guy who looks at your code. If your reasoning not to do them is "it takes a full minute of my time", then rethink your reasoning. It's perfectly fine to derive them without adding anything, any functionality or the like. Just deriving them to be able to distinct them.
– Aziuth
39 mins ago




Idea is to have control. You catch the exceptions that your code rises, and you do only catches for those that you expect. This tells people who read your code which exceptions can happen here. If you catch some generic exception, it can come from anywhere, from your code, from dependencies, who knows, not the guy who looks at your code. If your reasoning not to do them is "it takes a full minute of my time", then rethink your reasoning. It's perfectly fine to derive them without adding anything, any functionality or the like. Just deriving them to be able to distinct them.
– Aziuth
39 mins ago












@Aziuth Then why there is a logical division of errors in std. And why only one bad_alloc. Why not list_bad_alloc, vector_bad_alloc. That is more descriptive. Isn't it?
– Amit Bhaira
32 mins ago





@Aziuth Then why there is a logical division of errors in std. And why only one bad_alloc. Why not list_bad_alloc, vector_bad_alloc. That is more descriptive. Isn't it?
– Amit Bhaira
32 mins ago













5 Answers
5






active

oldest

votes

















up vote
4
down vote













Using the same exception everywhere is easy. Especially when trying to catch that exception. Unfortunately, it opens the door for Pokemon exception handling. It brings the risk of catching exceptions you don't expect.



Using a dedicated exception for all the different modules adds several advantages:



  • A custom message for each case, making it more useful for reporting

  • Catch can capture only the intended exceptions, more easily crashes on unexpected cases (yes, thats an advantage over incorrect handling)

  • On crash, IDE can show the exception type, helping even more with debugging





share|improve this answer



























    up vote
    3
    down vote













    Create your own exception types when:



    1. you might want to differentiate them when handling. If they're different types, you have the option to write different catch clauses. They should still have a common base so you can have common handling when that is appropriate

    2. you want to add some specific structured data you can actually use in the handler

    Having separate list and vector exceptions doesn't seem worthwhile, unless there's something distinctively list-like or vectorish about them. Are you really going to have different catch handling depending on which type of container had an error?



    Conversely it could make sense to have separate exception types for things that might be recoverable at runtime, versus things that are rolled-back but could be retried, versus things that are definitely fatal or indicate a bug.






    share|improve this answer



























      up vote
      1
      down vote













      I see two possible reasons.



      1) If you want to store in the exception class some custom information about the exception, then you need an exception with extra data members. Often you will inherit from one of the std exception classes.



      2) If you want to differentiate between exceptions. For instance suppose you have two distinct invalid argument situations and in your catch block you want to be able to differeentiate between the two. You can create two child classes of std::invalid_argument






      share|improve this answer



























        up vote
        1
        down vote













        The issue I see with each element of the STL firing a distinct memory exception is that some parts of the stl are built on others, so queue would have to catch and translate list exceptions, or clients of queue would have to know to handle list exceptions.



        I can see some logic in separating the exceptions from different modules, but I can also see the logic of having a project-wide exception base class. I can also see the logic of having exception type classes.



        The unholy alliance would be to build an exception hierarchy:



        std::exception
        EXProjectBase
        EXModuleBase
        EXModule1
        EXModule2
        EXClassBase
        EXClassMemory
        EXClassBadArg
        EXClassDisconnect
        EXClassTooSoon
        EXClassTooLate


        Then insist that every actual fired exception is derived from a module and an exception classification. Then, you can catch whatever makes sense to the catcher, such as a disconnection, rather than Having to separately catch HighLevelDisconnect and LowLevelDisconnect.



        On the other hand it is also completely fair to suggest that The HighLevel interface should have completely dealt with LowLevel failures, and they should not be seen by the HighLevel client.





        share



























          up vote
          0
          down vote













          I think that in addition to the reasons in other answers could be code readability, because a lot of time programmers spend supporting the code. Consider two pieces of code (assuming they throw "empty frame" error):



          void MyClass::function() nothrow(false) 

          // ...
          if (errorCondition)
          throw std::exception("Error: empty frame");




          void MyClass::function() nothrow(false) 

          // ...
          if (errorCondition)
          throw EmptyFrame();




          In the second case I think it is more readable, and the message for the user (which printed with what() function) is hidden inside this custom exception class.






          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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            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%2f52589224%2fwhen-should-you-create-your-own-exception-type%23new-answer', 'question_page');

            );

            Post as a guest






























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote













            Using the same exception everywhere is easy. Especially when trying to catch that exception. Unfortunately, it opens the door for Pokemon exception handling. It brings the risk of catching exceptions you don't expect.



            Using a dedicated exception for all the different modules adds several advantages:



            • A custom message for each case, making it more useful for reporting

            • Catch can capture only the intended exceptions, more easily crashes on unexpected cases (yes, thats an advantage over incorrect handling)

            • On crash, IDE can show the exception type, helping even more with debugging





            share|improve this answer
























              up vote
              4
              down vote













              Using the same exception everywhere is easy. Especially when trying to catch that exception. Unfortunately, it opens the door for Pokemon exception handling. It brings the risk of catching exceptions you don't expect.



              Using a dedicated exception for all the different modules adds several advantages:



              • A custom message for each case, making it more useful for reporting

              • Catch can capture only the intended exceptions, more easily crashes on unexpected cases (yes, thats an advantage over incorrect handling)

              • On crash, IDE can show the exception type, helping even more with debugging





              share|improve this answer






















                up vote
                4
                down vote










                up vote
                4
                down vote









                Using the same exception everywhere is easy. Especially when trying to catch that exception. Unfortunately, it opens the door for Pokemon exception handling. It brings the risk of catching exceptions you don't expect.



                Using a dedicated exception for all the different modules adds several advantages:



                • A custom message for each case, making it more useful for reporting

                • Catch can capture only the intended exceptions, more easily crashes on unexpected cases (yes, thats an advantage over incorrect handling)

                • On crash, IDE can show the exception type, helping even more with debugging





                share|improve this answer












                Using the same exception everywhere is easy. Especially when trying to catch that exception. Unfortunately, it opens the door for Pokemon exception handling. It brings the risk of catching exceptions you don't expect.



                Using a dedicated exception for all the different modules adds several advantages:



                • A custom message for each case, making it more useful for reporting

                • Catch can capture only the intended exceptions, more easily crashes on unexpected cases (yes, thats an advantage over incorrect handling)

                • On crash, IDE can show the exception type, helping even more with debugging






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 58 mins ago









                JVApen

                4,43011128




                4,43011128






















                    up vote
                    3
                    down vote













                    Create your own exception types when:



                    1. you might want to differentiate them when handling. If they're different types, you have the option to write different catch clauses. They should still have a common base so you can have common handling when that is appropriate

                    2. you want to add some specific structured data you can actually use in the handler

                    Having separate list and vector exceptions doesn't seem worthwhile, unless there's something distinctively list-like or vectorish about them. Are you really going to have different catch handling depending on which type of container had an error?



                    Conversely it could make sense to have separate exception types for things that might be recoverable at runtime, versus things that are rolled-back but could be retried, versus things that are definitely fatal or indicate a bug.






                    share|improve this answer
























                      up vote
                      3
                      down vote













                      Create your own exception types when:



                      1. you might want to differentiate them when handling. If they're different types, you have the option to write different catch clauses. They should still have a common base so you can have common handling when that is appropriate

                      2. you want to add some specific structured data you can actually use in the handler

                      Having separate list and vector exceptions doesn't seem worthwhile, unless there's something distinctively list-like or vectorish about them. Are you really going to have different catch handling depending on which type of container had an error?



                      Conversely it could make sense to have separate exception types for things that might be recoverable at runtime, versus things that are rolled-back but could be retried, versus things that are definitely fatal or indicate a bug.






                      share|improve this answer






















                        up vote
                        3
                        down vote










                        up vote
                        3
                        down vote









                        Create your own exception types when:



                        1. you might want to differentiate them when handling. If they're different types, you have the option to write different catch clauses. They should still have a common base so you can have common handling when that is appropriate

                        2. you want to add some specific structured data you can actually use in the handler

                        Having separate list and vector exceptions doesn't seem worthwhile, unless there's something distinctively list-like or vectorish about them. Are you really going to have different catch handling depending on which type of container had an error?



                        Conversely it could make sense to have separate exception types for things that might be recoverable at runtime, versus things that are rolled-back but could be retried, versus things that are definitely fatal or indicate a bug.






                        share|improve this answer












                        Create your own exception types when:



                        1. you might want to differentiate them when handling. If they're different types, you have the option to write different catch clauses. They should still have a common base so you can have common handling when that is appropriate

                        2. you want to add some specific structured data you can actually use in the handler

                        Having separate list and vector exceptions doesn't seem worthwhile, unless there's something distinctively list-like or vectorish about them. Are you really going to have different catch handling depending on which type of container had an error?



                        Conversely it could make sense to have separate exception types for things that might be recoverable at runtime, versus things that are rolled-back but could be retried, versus things that are definitely fatal or indicate a bug.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 1 hour ago









                        Useless

                        42.5k45193




                        42.5k45193




















                            up vote
                            1
                            down vote













                            I see two possible reasons.



                            1) If you want to store in the exception class some custom information about the exception, then you need an exception with extra data members. Often you will inherit from one of the std exception classes.



                            2) If you want to differentiate between exceptions. For instance suppose you have two distinct invalid argument situations and in your catch block you want to be able to differeentiate between the two. You can create two child classes of std::invalid_argument






                            share|improve this answer
























                              up vote
                              1
                              down vote













                              I see two possible reasons.



                              1) If you want to store in the exception class some custom information about the exception, then you need an exception with extra data members. Often you will inherit from one of the std exception classes.



                              2) If you want to differentiate between exceptions. For instance suppose you have two distinct invalid argument situations and in your catch block you want to be able to differeentiate between the two. You can create two child classes of std::invalid_argument






                              share|improve this answer






















                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                I see two possible reasons.



                                1) If you want to store in the exception class some custom information about the exception, then you need an exception with extra data members. Often you will inherit from one of the std exception classes.



                                2) If you want to differentiate between exceptions. For instance suppose you have two distinct invalid argument situations and in your catch block you want to be able to differeentiate between the two. You can create two child classes of std::invalid_argument






                                share|improve this answer












                                I see two possible reasons.



                                1) If you want to store in the exception class some custom information about the exception, then you need an exception with extra data members. Often you will inherit from one of the std exception classes.



                                2) If you want to differentiate between exceptions. For instance suppose you have two distinct invalid argument situations and in your catch block you want to be able to differeentiate between the two. You can create two child classes of std::invalid_argument







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 1 hour ago









                                Fabio

                                632315




                                632315




















                                    up vote
                                    1
                                    down vote













                                    The issue I see with each element of the STL firing a distinct memory exception is that some parts of the stl are built on others, so queue would have to catch and translate list exceptions, or clients of queue would have to know to handle list exceptions.



                                    I can see some logic in separating the exceptions from different modules, but I can also see the logic of having a project-wide exception base class. I can also see the logic of having exception type classes.



                                    The unholy alliance would be to build an exception hierarchy:



                                    std::exception
                                    EXProjectBase
                                    EXModuleBase
                                    EXModule1
                                    EXModule2
                                    EXClassBase
                                    EXClassMemory
                                    EXClassBadArg
                                    EXClassDisconnect
                                    EXClassTooSoon
                                    EXClassTooLate


                                    Then insist that every actual fired exception is derived from a module and an exception classification. Then, you can catch whatever makes sense to the catcher, such as a disconnection, rather than Having to separately catch HighLevelDisconnect and LowLevelDisconnect.



                                    On the other hand it is also completely fair to suggest that The HighLevel interface should have completely dealt with LowLevel failures, and they should not be seen by the HighLevel client.





                                    share
























                                      up vote
                                      1
                                      down vote













                                      The issue I see with each element of the STL firing a distinct memory exception is that some parts of the stl are built on others, so queue would have to catch and translate list exceptions, or clients of queue would have to know to handle list exceptions.



                                      I can see some logic in separating the exceptions from different modules, but I can also see the logic of having a project-wide exception base class. I can also see the logic of having exception type classes.



                                      The unholy alliance would be to build an exception hierarchy:



                                      std::exception
                                      EXProjectBase
                                      EXModuleBase
                                      EXModule1
                                      EXModule2
                                      EXClassBase
                                      EXClassMemory
                                      EXClassBadArg
                                      EXClassDisconnect
                                      EXClassTooSoon
                                      EXClassTooLate


                                      Then insist that every actual fired exception is derived from a module and an exception classification. Then, you can catch whatever makes sense to the catcher, such as a disconnection, rather than Having to separately catch HighLevelDisconnect and LowLevelDisconnect.



                                      On the other hand it is also completely fair to suggest that The HighLevel interface should have completely dealt with LowLevel failures, and they should not be seen by the HighLevel client.





                                      share






















                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote









                                        The issue I see with each element of the STL firing a distinct memory exception is that some parts of the stl are built on others, so queue would have to catch and translate list exceptions, or clients of queue would have to know to handle list exceptions.



                                        I can see some logic in separating the exceptions from different modules, but I can also see the logic of having a project-wide exception base class. I can also see the logic of having exception type classes.



                                        The unholy alliance would be to build an exception hierarchy:



                                        std::exception
                                        EXProjectBase
                                        EXModuleBase
                                        EXModule1
                                        EXModule2
                                        EXClassBase
                                        EXClassMemory
                                        EXClassBadArg
                                        EXClassDisconnect
                                        EXClassTooSoon
                                        EXClassTooLate


                                        Then insist that every actual fired exception is derived from a module and an exception classification. Then, you can catch whatever makes sense to the catcher, such as a disconnection, rather than Having to separately catch HighLevelDisconnect and LowLevelDisconnect.



                                        On the other hand it is also completely fair to suggest that The HighLevel interface should have completely dealt with LowLevel failures, and they should not be seen by the HighLevel client.





                                        share












                                        The issue I see with each element of the STL firing a distinct memory exception is that some parts of the stl are built on others, so queue would have to catch and translate list exceptions, or clients of queue would have to know to handle list exceptions.



                                        I can see some logic in separating the exceptions from different modules, but I can also see the logic of having a project-wide exception base class. I can also see the logic of having exception type classes.



                                        The unholy alliance would be to build an exception hierarchy:



                                        std::exception
                                        EXProjectBase
                                        EXModuleBase
                                        EXModule1
                                        EXModule2
                                        EXClassBase
                                        EXClassMemory
                                        EXClassBadArg
                                        EXClassDisconnect
                                        EXClassTooSoon
                                        EXClassTooLate


                                        Then insist that every actual fired exception is derived from a module and an exception classification. Then, you can catch whatever makes sense to the catcher, such as a disconnection, rather than Having to separately catch HighLevelDisconnect and LowLevelDisconnect.



                                        On the other hand it is also completely fair to suggest that The HighLevel interface should have completely dealt with LowLevel failures, and they should not be seen by the HighLevel client.






                                        share











                                        share


                                        share










                                        answered 8 mins ago









                                        Gem Taylor

                                        1,425115




                                        1,425115




















                                            up vote
                                            0
                                            down vote













                                            I think that in addition to the reasons in other answers could be code readability, because a lot of time programmers spend supporting the code. Consider two pieces of code (assuming they throw "empty frame" error):



                                            void MyClass::function() nothrow(false) 

                                            // ...
                                            if (errorCondition)
                                            throw std::exception("Error: empty frame");




                                            void MyClass::function() nothrow(false) 

                                            // ...
                                            if (errorCondition)
                                            throw EmptyFrame();




                                            In the second case I think it is more readable, and the message for the user (which printed with what() function) is hidden inside this custom exception class.






                                            share|improve this answer


























                                              up vote
                                              0
                                              down vote













                                              I think that in addition to the reasons in other answers could be code readability, because a lot of time programmers spend supporting the code. Consider two pieces of code (assuming they throw "empty frame" error):



                                              void MyClass::function() nothrow(false) 

                                              // ...
                                              if (errorCondition)
                                              throw std::exception("Error: empty frame");




                                              void MyClass::function() nothrow(false) 

                                              // ...
                                              if (errorCondition)
                                              throw EmptyFrame();




                                              In the second case I think it is more readable, and the message for the user (which printed with what() function) is hidden inside this custom exception class.






                                              share|improve this answer
























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                I think that in addition to the reasons in other answers could be code readability, because a lot of time programmers spend supporting the code. Consider two pieces of code (assuming they throw "empty frame" error):



                                                void MyClass::function() nothrow(false) 

                                                // ...
                                                if (errorCondition)
                                                throw std::exception("Error: empty frame");




                                                void MyClass::function() nothrow(false) 

                                                // ...
                                                if (errorCondition)
                                                throw EmptyFrame();




                                                In the second case I think it is more readable, and the message for the user (which printed with what() function) is hidden inside this custom exception class.






                                                share|improve this answer














                                                I think that in addition to the reasons in other answers could be code readability, because a lot of time programmers spend supporting the code. Consider two pieces of code (assuming they throw "empty frame" error):



                                                void MyClass::function() nothrow(false) 

                                                // ...
                                                if (errorCondition)
                                                throw std::exception("Error: empty frame");




                                                void MyClass::function() nothrow(false) 

                                                // ...
                                                if (errorCondition)
                                                throw EmptyFrame();




                                                In the second case I think it is more readable, and the message for the user (which printed with what() function) is hidden inside this custom exception class.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 14 mins ago

























                                                answered 24 mins ago









                                                stackoverflower

                                                6811




                                                6811



























                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52589224%2fwhen-should-you-create-your-own-exception-type%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    Comments

                                                    Popular posts from this blog

                                                    What does second last employer means? [closed]

                                                    List of Gilmore Girls characters

                                                    Confectionery