No it’s not an assembly language

I keep coming across articles that talk about C# being the assembly language of the .NET platform. Sadly though, C# is just a lens through which we look at the platform, and there are various places where C# doesn’t express things quite as well as it might.

In particular, IL has a try…fault… construct, where the fault block is only executed when an exception is making its way out of the try body. This is much like

try
{
    …. body ….
}
catch (Exception)
{
    Console.WriteLine(“Fault”);
    throw;
}

However, a change was made in the .NET 4 platform, to avoid unmanaged exceptions getting caught by C# code, which was probably not expecting them,

If we write some C code to generate an exception:

extern “C” int __declspec(dllexport) CauseAccessViolation()
{
    int *x = 0;
    return *x;
}

and call it from C#

[DllImport(“CauseAccessViolation.dll”)]
static extern int CauseAccessViolation();

static void Main(string[] args)
{
    try
    {
        CauseAccessViolation();
    }
    catch (Exception)
    {
        Console.WriteLine(“Fault”);
        throw;
    }
}

Under .NET 2, the “Fault” will be printed, but under .NET 4 it won’t. Confusingly, replacing the unmanaged call with a throw of the exception that it raises, AccessViolationException, both platforms print “Fault”. This behaviour can be configured using the application’s configuration file, but to me it feels dirty that some Exceptions are being treated specially.

Advertisements
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s