[Mono-bugs] [Bug 24103] New - finally blocks running at incorrect time

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
2 May 2002 03:27:54 -0000


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by linus@linus.com.

http://bugzilla.ximian.com/show_bug.cgi?id=24103

--- shadow/24103	Wed May  1 23:27:54 2002
+++ shadow/24103.tmp.7603	Wed May  1 23:27:54 2002
@@ -0,0 +1,80 @@
+Bug#: 24103
+Product: Mono/Runtime
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Critical
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: linus@linus.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: finally blocks running at incorrect time
+
+Mono executes finally blocks as the stack is unwound but .Net only runs 
+finally blocks after a matching catch filter is found. This difference 
+means that delegates of the AppDomain.UnhandledException event will be 
+called at different times in mono and .Net.
+
+Here is a test case that can be used to see the different behaviors:
+
+// 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.