Throwing exceptions in a constructor is perfectly fine in .Net.  And in many
circumstances, it is the Right Thing.

If a constructor cannot build a valid instance of a class, then it should
throw an exception -- just like any other method.

One key difference between virtual methods in .Net and in C++ is that, in
.Net, the vtable for an object is fixed.  As soon as the GC allocates space
for the object, the CLR writes the vtable pointer (really, the type pointer)
into the object header, and it NEVER changes after that point.  You don't
run into all of the ridiculous behaviors that you do in C++, with virtual
function slots changing as you construct or destruct objects.

By all means, if a constructor can't complete its work -- throw an
exception!  And, yes, the code that called "new foo()" will necessary not
receive a reference to the object.  That's by design.

-- arlie



-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Adam Straughan
Sent: Thursday, April 01, 2004 2:50 AM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] 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