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. Exception handling in Constructor

Exception handling in Constructor

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 5 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.
  • R Offline
    R Offline
    ragavan
    wrote on last edited by
    #1

    Hi Does anyone know how to do exception handling in constructor?

    CPalliniC S realJSOPR N 4 Replies Last reply
    0
    • R ragavan

      Hi Does anyone know how to do exception handling in constructor?

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      try and catch block aren't enough for you? :-D

      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.

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • R ragavan

        Hi Does anyone know how to do exception handling in constructor?

        S Offline
        S Offline
        shiraztk
        wrote on last edited by
        #3

        Hi Make the constructors private, then create a static member function called InitYourObject(). CYourClass * CYourClass::InitYourObject() { try { CYourClass *object = new CYourClass; return object; } catch(...) { //handle the error return NULL; // or throw exception } } To create an object call CYourClass *myObject = CYourClass::InitObject(); Hope it helps Regards

        The Best Religion is Science. Once you understand it, you will know God.

        1 Reply Last reply
        0
        • R ragavan

          Hi Does anyone know how to do exception handling in constructor?

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

          I avoid putting error handing in the constructor (short of the occasional ASSERT). The constructor is for constructing. If there's a possibility of bad/invalid data, you should either allow the construction to happen and set appropriate conditions in the object to indicate invalid data, or you should validate data BEFORE construction so you can assume that everything is okay at the time of construction. I feel it makes the code much more maintainable (with appropriate and copious code comments of course). Of course, this is just the way I do it and I'm sure others will want to argue their own approach. You guys have fun with that.

          "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
          • R ragavan

            Hi Does anyone know how to do exception handling in constructor?

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            Ask the Guru[^]


            Programming Blog utf8-cpp

            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