-1

We know that the code in the finally block is guaranteed to execute, regardless of whether the exception is caught. But let's say we have the following code:

class Program {
   static void Main(string[] args) {
      try {
         int a = 0;
         int b = 2021 / a;
      }
      finally {
         System.Diagnostics.Debug.WriteLine("finally execute");
      }
   }
}

I cannot use Console.WriteLine since an unhandled exception terminates a process, no output written to the console, so I use System.Diagnostics.Debug.WriteLine, hopefully I can see "finally execute" displayed in the VS debug window.

When I execute the code, I didn't find "finally execute" in debug window, which means finally block won't execute if there is an unhandled exception.

If you look at this link https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-finally

It does say:

Part one

Within a handled exception, the associated finally block is guaranteed to be run. However, if the exception is unhandled, execution of the finally block is dependent on how the exception unwind operation is triggered. That, in turn, is dependent on how your computer is set up.

Part Two

The only cases where finally clauses don't run involve a program being immediately stopped. An example of this would be when InvalidProgramException gets thrown because of the IL statements being corrupt. On most operating systems, reasonable resource cleanup will take place as part of stopping and unloading the process.

My questions are:

Q1-What does "dependent on how your computer is set up" in Part One mean?

Q2-If it is dependent on how the computer is set up, then what does Part Two mean? The exception in my example is DivideByZeroException, not InvalidProgramException, so why the finllay block still doesn't execute

MickyD
  • 14,343
  • 6
  • 43
  • 67
amjad
  • 3,048
  • 1
  • 11
  • 42
  • See: https://stackoverflow.com/a/60158325/1043380 – gunr2171 Apr 01 '21 at 03:09
  • [Related?](https://stackoverflow.com/questions/32955393/how-to-debug-a-finally-block-in-visual-studio) – DiplomacyNotWar Apr 01 '21 at 03:10
  • 1
    I think the problem is specifically for the Main method. – gunr2171 Apr 01 '21 at 03:11
  • 2
    _"I cannot use Console.WriteLine since an unhandled exception terminates a process"_ - in your example that is incorrect. I just tried your code substituting `Console.WriteLine` for `Debug.WriteLine` without issue. It wrote to the console. Additonally, your code is not subject to immediate termination. Code _after_ the `finally` block continues to run – MickyD Apr 01 '21 at 03:15
  • @MickyD so does your finally block execute? – amjad Apr 01 '21 at 03:17
  • 2
    That's what I just said. Be sure to run your app _outside_ of the debugger in case you get confused with the _first-chance exception debugger_ – MickyD Apr 01 '21 at 03:18
  • @MickyD what does " immediate termination: mean? could you show me your complete code? – amjad Apr 01 '21 at 03:22

1 Answers1

0

As per my understanding finally will execute in this case. As you have not use Catch block it throw unhandled exception to the process level so any statement after finally block will not execute.

This is the code.

using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int a = 0;
                int b = 2021 / a;
            }
            finally
            {
                Console.WriteLine("finally execute");
            }
            Console.WriteLine("Complete");
            Console.ReadLine();
        }
    }
}

Sample output. enter image description here

dotnetstep
  • 16,207
  • 4
  • 51
  • 66
  • 1
    (Just to add on) The key takeaway is that the finally _will_ execute if you run _outside_ of a debugger. – gunr2171 Apr 01 '21 at 03:39
  • @gunr2171 so how does debug suppress the execution of finally block in unhandled exception? – amjad Apr 01 '21 at 03:53