Exception in a constructor

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

    #1

    Exception in a constructor

    If an exception occurs or is thrown in my constructor, what does the
    constructor actually return? A null?


  • Chris R. Timmons

    #2
    Re: Exception in a constructor

    "JSheble" <jsheble-NOSPAM@logicor. com> wrote in
    news:uHEgyG7XFH A.2348@TK2MSFTN GP14.phx.gbl:
    [color=blue]
    > If an exception occurs or is thrown in my constructor, what does
    > the constructor actually return? A null?[/color]

    You can easily write a small program to find out.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.

    Comment

    • Bob Grommes

      #3
      Re: Exception in a constructor

      Constructors don't return anything as they are essentially void methods. I
      think what you're asking is whether the object reference returned by a "new"
      operation will be null if an exception is thrown in the constructor of the
      object being created.

      What would happen is the exception will bubble up to the method that's
      newing the object. If you trap the exception in the constructor then I
      think from the viewpoint of the calling program that the constructor would
      have finished doing its work. Unlike, say, FoxPro where the constructor can
      return a bool true or false to signify whether the object was successfully
      constructed, the CLR has no such concept. You should whip up a quick test
      program to verify this, as I'm only 90% sure on this, but I think your best
      bet is probably to let the exception bubble up to the caller and use a try
      block around the "new" call:

      try {
      someVar = new SomeClass();
      } catch (Exception ex) {
      // handle exception
      }

      This costs nothing in terms of performance unless the exception is actually
      thrown. If that will be a common thing then you might want to find some way
      to check for the error condition in the constructor without throwing an
      exception and set a flag in the class itself, e.g.,

      someVar = new SomeClass();

      if (someVar.IsVali d == false) {
      // handle exception
      }

      --Bob

      "JSheble" <jsheble-NOSPAM@logicor. com> wrote in message
      news:uHEgyG7XFH A.2348@TK2MSFTN GP14.phx.gbl...[color=blue]
      > If an exception occurs or is thrown in my constructor, what does the
      > constructor actually return? A null?
      >[/color]


      Comment

      • Helge Jensen

        #4
        Re: Exception in a constructor



        JSheble wrote:[color=blue]
        > If an exception occurs or is thrown in my constructor, what does the
        > constructor actually return? A null?[/color]

        No, it does not return -- it throws ;)

        I'm guessing you are coming from C++, where constructors could return
        null. Even C++ has changed that so you have to use

        T* t = new (nothrow) T();

        to have T() return null on allocation-errors, according to recent
        specifications.

        --
        Helge Jensen
        mailto:helge.je nsen@slog.dk
        sip:helge.jense n@slog.dk
        -=> Sebastian cover-music: http://ungdomshus.nu <=-

        Comment

        • Bruce Wood

          #5
          Re: Exception in a constructor

          I think that it doesn't matter, because there's no way for you to get
          your hands on the return value, anyway. Think of it this way:

          MyClass myVar = null;
          try
          {
          myVar = new MyClass();
          }
          catch (Exception ex)
          {
          Console.WriteLi ne("myVar is {0}", myVar == null ? "(null)" :
          myVar);
          }

          What will the WriteLine write out? Always "(null)". Why? Because
          regardless of what the "new" operation returns, the assignment
          operation "myVar =" will never be executed, because the constructor
          threw an exception, so execution will continue immediately with the
          Console.WriteLi ne statement, and myVar will never be altered.

          The bottom line is that there is no way to instruct the compiler to
          instruct the CLR to complete the assignment operation if the
          constructor throws an exception, so even if the "new" returns a
          partially constructed object, you can't assign that to anything.

          Comment

          Working...