exception from constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher

    exception from constructor

    There is a rumor around the office that it is unacceptable to throw an
    exception from a constructor.
    I have always thrown exceptions from constuctors. It is the only
    method of handling errors!

    Where in the world is this argument coming from?
    Does it have any validity?

    I seek knowledge o gurus of the C++
  • acehreli@gmail.com

    #2
    Re: exception from constructor

    On Jul 31, 4:59 pm, Christopher <cp...@austin.r r.comwrote:
    There is a rumor around the office that it is unacceptable to throw an
    exception from a constructor.
    What do they recommend for when it's impossible to construct the
    object?
    I have always thrown exceptions from constuctors. It is the only
    method of handling errors!
    Of course. It is either an object or not.
    Where in the world is this argument coming from?
    Does it have any validity?
    It comes from code that is not exception safe. In that case, any
    exception can leak resources. Banning exceptions in constructors is an
    attempt to get away from such problems.

    Ali

    Comment

    • Markus Moll

      #3
      Re: exception from constructor

      Hi

      Christopher wrote:
      There is a rumor around the office that it is unacceptable to throw an
      exception from a constructor.
      I have always thrown exceptions from constuctors. It is the only
      method of handling errors!
      >
      Where in the world is this argument coming from?
      Does it have any validity?
      To me, it sounds like they confuse constructors and destructors.
      It is rarely a good idea to throw within a destructor.


      Markus

      Comment

      • James Kanze

        #4
        Re: exception from constructor

        On Aug 1, 2:12 am, acehr...@gmail. com wrote:
        On Jul 31, 4:59 pm, Christopher <cp...@austin.r r.comwrote:
        There is a rumor around the office that it is unacceptable
        to throw an exception from a constructor.
        What do they recommend for when it's impossible to construct
        the object?
        An assertion failure? Constructing the object with an invalid
        state, which you test before each use?

        In all but a few, very rare cases, of course, exceptions are
        preferable to these alternatives.

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        • Juha Nieminen

          #5
          Re: exception from constructor

          James Kanze wrote:
          No, the proper way of implementing the constructor is, as I
          said, to replace the int* with std::vector< int >.
          The problem is if std::vector, or any other STL container, does not do
          what you want, and you are creating your own data container.

          If this is the case, at some point in your code you must explicitly
          write the 'new', and you have to be careful that it doesn't leak in the
          case of an exception.

          Comment

          • James Kanze

            #6
            Re: exception from constructor

            On Aug 2, 10:16 am, Juha Nieminen <nos...@thanks. invalidwrote:
            James Kanze wrote:
            No, the proper way of implementing the constructor is, as I
            said, to replace the int* with std::vector< int >.
            The problem is if std::vector, or any other STL container,
            does not do what you want,
            Then you create one that does.
            and you are creating your own data container.
            Then you don't have a second element whose constructor might
            throw.
            If this is the case, at some point in your code you must
            explicitly write the 'new', and you have to be careful that it
            doesn't leak in the case of an exception.
            There are obviously many cases where you need an explicit new.
            That doesn't change anything; whether you use a standard
            container, or write some container or smart pointer or whatever
            of your own. The point is that any given class can only be
            responsible for one resource. The principle of RAII is that the
            responsibility for a resource is always in the hands of a single
            class whose sole role is to be responsible for that resource.

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            Working...