[Mono-list] RFC: Corlib Unit Test on Linux How-To

Linus Upson linus@linus.com
Tue, 30 Apr 2002 15:17:55 -0700


Here is a test case that shows that the .Net runtime looks for a
matching catch block before executing the finally blocks:

// FinallyTest.cs

using System;

public class FinallyTest {
    public static void MyHandler(object sender,
        UnhandledExceptionEventArgs args) {

        Console.WriteLine("UnhandledExceptionEventHandler called");
    }

    public static void Main() {
        Console.WriteLine("Top level block");

        AppDomain domain = AppDomain.CurrentDomain;
        domain.UnhandledException +=
            new UnhandledExceptionEventHandler(MyHandler);

        try {
            Console.WriteLine("First try block");
            try {
                Console.WriteLine("Second try block");
                throw new Exception();
            } finally {
                Console.WriteLine("Second finally block");
            }
        } finally {
            Console.WriteLine("First finally block");
        }

        Console.WriteLine("Back to top level block");
    }
}

When compiled and run on .Net it outputs the following:

$ FinallyTest.exe
Top level block
First try block
Second try block
UnhandledExceptionEventHandler called
Second finally block
First finally block
(null)

Without the custom UnhandledExceptionEventHandler installed, you get the
standard unhandled exception message between the second try block and
the second finally block. .Net also brings up a panel asking if you want
to debug the program. Only after you dismiss the panel do the finally
blocks run.

I'm not sure why they chose to do this, but it does look intentional. My
only guess so far is that they wanted to allow developers the
opportunity to inspect the program's state with a debugger before any of
the finally blocks ran.

Linus


-----Original Message-----
From: mono-list-admin@ximian.com [mailto:mono-list-admin@ximian.com] On
Behalf Of Martijn Stegeman
Sent: Tuesday, April 30, 2002 10:25 AM
To: Mono List
Subject: RE: [Mono-list] RFC: Corlib Unit Test on Linux How-To


"The code inside the finally block is guaranteed to execute before the
exit
of the function, whether there is an exception or not." - Eric
Gunnerson, A
Programmer's Introduction to C#.

> -----Original Message-----
> From: mono-list-admin@ximian.com [mailto:mono-list-admin@ximian.com]On
> Behalf Of Charles Wiltgen
> Sent: Tuesday, April 30, 2002 7:01 PM
> To: Mono List
> Subject: Re: [Mono-list] RFC: Corlib Unit Test on Linux How-To
>
>
> Dietmar Maurer wrote...
>
> >> I have a vague recollection of reading somewhere that .Net doesn't
run
> >> finally clauses until it finds a matching catch block for the
> exception.
> >
> > really - that sounds strange.
>
> "Applied Microsoft .NET Framework Programming" says that finally is
> guaranteed to execute no mater what kind of exception is thrown,
whether
> or not the try statement has any catch blocks.
>
> --
> Charles Wiltgen
>
>   "Love is a snowmobile racing across the tundra and
>    then suddenly it flips over, pinning you underneath.
>    At night, the ice weasels come." - Nietzsche (Groening)