Huh?  Isn't that the idea, Adam -- that 'a' remains null?  It would be awful
if 'a' came away from that in some not-null, ambiguous,
partially-constructed state.  Right?  (That was the problem with C++, which
the original poster alluded to.)

At the IL level, the instantiation of A (newobj) and the assignment of the
resulting reference to the MyClass::a field (stfld) are two separate
instructions.

The fact that they can share a single line of C# code is an irrelevant
convenience.

   this.a = new A();

is the same as

   A temp = new A();
   this.a = temp;

Cheers,
-Shawn
http://www.windojitsu.com/


-----Original Message-----
From: Adam Straughan [mailto:[EMAIL PROTECTED]
Sent: Wednesday, March 31, 2004 23:50
Subject: Re: Exceptions in Constructors

Its still got bad idea stamped all over it.  The object would not get
constructed, this is almost certainly doc'd somewhere.

The following code outputs the string "null"

using System;

public class MyClass
{
        public static void Main()
        {
        A a = null;
        try
        {
            a = new A();
        }
        catch(Exception){}
        if (a==null)
            Console.WriteLine("null");
        else
            Console.WriteLine("not null");
        }
}
class A
{
    public A()
    {
        throw new Exception("");
    }
}




> -----Original Message-----
> From: Chris Snyder [mailto:[EMAIL PROTECTED]
> Sent: 31 March 2004 15:30
> To: [EMAIL PROTECTED]
> Subject: [ADVANCED-DOTNET] Exceptions in Constructors
>
> I have a (hopefully) simple question.
>
> In a nutshell, as a C++ developer, various people hammered
> into my brain that throwing an exception in a constructor
> (whether intentionally or by performing some action which
> would cause an exception to be thrown) was extremely bad.
> However, my research seems to indicate that .NET handles
> constructors differently, allowing developers to perform
> actions in a constructor that may result in exceptions being
> thrown, or to <gasp> even throw an exception themselves.
>
> Thoughts?
>
> Thanks a lot,
> Chris Snyder
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
> Some .NET courses you may be interested in:
>
> NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
> http://www.develop.com/courses/gaspdotnetls
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>

===================================
This list is hosted by DevelopMentorR  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to