Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Exceptions in destructors

Exceptions in destructors

Scheduled Pinned Locked Moved C / C++ / MFC
question
16 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • realJSOPR realJSOP

    Throwing exceptions from constructors or destructors is a bad idea.

    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
    -----
    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #4

    John Simmons / outlaw programmer wrote:

    Throwing exceptions from constructors or destructors is a bad idea

    I don't think so. BTW the OP snipped doesn't show an exception thrown from the contructor, it shows an exception handled inside it. :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

    1 Reply Last reply
    0
    • T tom groezer

      Will the following code get rid of any exception that might be thrown in a destructor Session::~Session() { try { logDestruction(this); } catch (...) { } } What are the scenarios in which a destructor is likely to riase an exception? I have not seen in any prodution code with a destrcutor having a try/catch clause like above? Comments

      S Offline
      S Offline
      skornel0
      wrote on last edited by
      #5

      I have used similar code in my remote communications classes. My DTOR will close the comm connection, but if the reason the DTOR is being called is because the connection was already broken/disconnected, I needed to ensure that no exception interrupted the normal DTOR chain. It may not be quite what the C++ designers had in mind but it works. At least with MS VC++. Besides, if they didn't want it to work, it woud have explicitly stated so in the ARM.

      1 Reply Last reply
      0
      • realJSOPR realJSOP

        Throwing exceptions from constructors or destructors is a bad idea.

        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #6

        John Simmons / outlaw programmer wrote:

        Throwing exceptions from constructors or destructors is a bad idea.

        Huh? :confused:;)

        Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

        realJSOPR B T 3 Replies Last reply
        0
        • M Mark Salsbery

          John Simmons / outlaw programmer wrote:

          Throwing exceptions from constructors or destructors is a bad idea.

          Huh? :confused:;)

          Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #7

          It's just something I don't do.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          • M Mark Salsbery

            John Simmons / outlaw programmer wrote:

            Throwing exceptions from constructors or destructors is a bad idea.

            Huh? :confused:;)

            Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

            B Offline
            B Offline
            bob16972
            wrote on last edited by
            #8

            I don't know about the ctors but for the dtors, reference Effective C++ Third Edition by Scott Meyers. Specifically, "Item 8: Prevent exceptions from leaving destructors" There seem to be many schools of thought on this type of issue so I'm not claiming this is the gospel truth but it's worth a read.

            M 1 Reply Last reply
            0
            • B bob16972

              I don't know about the ctors but for the dtors, reference Effective C++ Third Edition by Scott Meyers. Specifically, "Item 8: Prevent exceptions from leaving destructors" There seem to be many schools of thought on this type of issue so I'm not claiming this is the gospel truth but it's worth a read.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #9

              I wasn't necessarily agreeing or disagreeing with John's comment. I meant more to open up a discussion. As far as destructors goes, I agree. It makes no sense. Destructors aren't called explicitly and when they are called, one would have to have complete knowledge of when. It could be during the unwinding of another exception. It doesn't even make sense for something exceptional to happen during destruction anyway. For constructors, I don't throw exceptions either. Maybe I'm old-school, but I prefer a two-part construction/initialization if error handling is necessary. For constructors I don't agree that one shouldn't throw exceptions. I don't think exceptions should be used for error handling, nor do I think they were meant to be used that way. IMO they were meant for exceptional cirumstances - something that happens out of the programmer and user's control that is unrecoverable. For framework/library development, however, where a programmer may use a class the wrong way, it may be ok. An exception during the design/implementation phase can be useful to me at that stage, maybe providing enough info to show me immediately what I did wrong. Just my rambling 2-cents :) Cheers, Mark

              Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

              P 1 Reply Last reply
              0
              • T tom groezer

                Will the following code get rid of any exception that might be thrown in a destructor Session::~Session() { try { logDestruction(this); } catch (...) { } } What are the scenarios in which a destructor is likely to riase an exception? I have not seen in any prodution code with a destrcutor having a try/catch clause like above? Comments

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #10

                Check this[^] out.

                Steve

                1 Reply Last reply
                0
                • M Mark Salsbery

                  I wasn't necessarily agreeing or disagreeing with John's comment. I meant more to open up a discussion. As far as destructors goes, I agree. It makes no sense. Destructors aren't called explicitly and when they are called, one would have to have complete knowledge of when. It could be during the unwinding of another exception. It doesn't even make sense for something exceptional to happen during destruction anyway. For constructors, I don't throw exceptions either. Maybe I'm old-school, but I prefer a two-part construction/initialization if error handling is necessary. For constructors I don't agree that one shouldn't throw exceptions. I don't think exceptions should be used for error handling, nor do I think they were meant to be used that way. IMO they were meant for exceptional cirumstances - something that happens out of the programmer and user's control that is unrecoverable. For framework/library development, however, where a programmer may use a class the wrong way, it may be ok. An exception during the design/implementation phase can be useful to me at that stage, maybe providing enough info to show me immediately what I did wrong. Just my rambling 2-cents :) Cheers, Mark

                  Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                  P Offline
                  P Offline
                  Paul Conrad
                  wrote on last edited by
                  #11

                  Mark Salsbery wrote:

                  Maybe I'm old-school, but I prefer a two-part construction/initialization if error handling is necessary.

                  I must be old school, too, because that is along the lines of my thinking. I have never put exception handling in the constructors/destructors and I don't plan on doing so. That's my 2 cents :-D

                  "The clue train passed his station without stopping." - John Simmons / outlaw programmer

                  1 Reply Last reply
                  0
                  • M Mark Salsbery

                    John Simmons / outlaw programmer wrote:

                    Throwing exceptions from constructors or destructors is a bad idea.

                    Huh? :confused:;)

                    Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                    T Offline
                    T Offline
                    ThatsAlok
                    wrote on last edited by
                    #12

                    Mark Salsbery wrote:

                    Mark Salsbery Microsoft MVP - Visual C++

                    congrats for becoming MVP!

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief

                    M 1 Reply Last reply
                    0
                    • T ThatsAlok

                      Mark Salsbery wrote:

                      Mark Salsbery Microsoft MVP - Visual C++

                      congrats for becoming MVP!

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #13

                      Thank you Alok! Are you a Visual C++ MVP?? Mark

                      Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                      T 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        Thank you Alok! Are you a Visual C++ MVP?? Mark

                        Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                        T Offline
                        T Offline
                        ThatsAlok
                        wrote on last edited by
                        #14

                        Mark Salsbery wrote:

                        Are you a Visual C++ MVP??

                        yeah i am Visual C++ MVP too! [:)] but it is secret

                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                        cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief

                        M 1 Reply Last reply
                        0
                        • T ThatsAlok

                          Mark Salsbery wrote:

                          Are you a Visual C++ MVP??

                          yeah i am Visual C++ MVP too! [:)] but it is secret

                          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #15

                          ThatsAlok wrote:

                          but it is secret

                          Well if I knew about it I'd say Congratulations to you as well! And I'd say I noticed you have been an MVP for a while. But since it's a secret, I know nothing :) Cheers! Mark

                          Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                          T 1 Reply Last reply
                          0
                          • M Mark Salsbery

                            ThatsAlok wrote:

                            but it is secret

                            Well if I knew about it I'd say Congratulations to you as well! And I'd say I noticed you have been an MVP for a while. But since it's a secret, I know nothing :) Cheers! Mark

                            Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

                            T Offline
                            T Offline
                            ThatsAlok
                            wrote on last edited by
                            #16

                            Mark Salsbery wrote:

                            And I'd say I noticed you have been an MVP for a while.

                            i am mvp for past two year.. but still doing soul searching for same [:)].. he he he it nice to see you here

                            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                            cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups