C#/specification bug?

C

cristalink

C# Language Specification 1.2 says:

<<Exceptions that occur during destructor execution are worth special
mention. If an exception occurs during destructor execution, and that
exception is not caught, then the execution of that destructor is terminated
and the destructor of the base class (if any) is called. If there is no base
class (as in the case of the object type) or if there is no base class
destructor, then the exception is discarded.>>


The exception does not seem to be discarded - the program terminates
abnormally whether B inherits A or not.

Cheers

---



using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace cstest
{
class A
{
~A()
{
Console.WriteLine( "~A()" );
}
}

class B //: A
{
~B()
{
throw new Exception();
//Console.WriteLine( "~B()" );
}

public bool z = false;
}
class Program
{
static void Main( string[] args )
{
B b = new B();
b = null;
#if true
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine( "OK" );
#else
int length = 2;
for( int i = 0; ; i++ )
{
Thread.Sleep( 1000 );
byte[] bb = new byte[ length ];
length *= 2;
Console.WriteLine( "OK {0} {1}", i, bb.Length );
}
#endif
}
}
}
 
L

Laura T.

It was/is true for .NET v1.1.
As of .NET 2.0 the exception will be propagated and kills the finalizer
thread.
And that will cause abnormal application termination.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top