4

We've seen many questions about try-catch-finally and try-finally constructions on this forum.

The number of answers increases the number of questions, so I have few too.

Here's a link into Microsoft explanation try-finally construction. I've already read it!

In the following article writes:

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.

  1. Am i correctly understand that in try-catch-finally construction finally will always be executed? (Excluding Environment.FastFail())

    I've read about StackOverFlowException (that finally block isn't executed in this occasion) on this forum, but when I throw it, the finally block is executed. So What's about StackOverFlowException ?

  2. Why finally block is not called?(In the code below)?

  3. For which cases we generally use try-finally?

  4. From what PC setting the finally block is depended?


using System;
namespace ConsoleApplication1
{
class Program
    {
    static void Main(string[] args)
        {
            try
            {
                throw  new Exception(); 
            }                              
            finally
            {
                Console.WriteLine("finally");
                Console.ReadKey();
            }
        }
    }
}    
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380

3 Answers3

6
  1. Yes, in most cases a finally-block will always be executed if you don't abort the execution with something like Environment.Exit(0) or Application.Exit() (like Common Man mentioned in his answer).

    For a StackOverFlowException and other deep application crashes it can't run as when the stack is full there is no memory left in this thread to execute any normal operation. Thus when you throw the exception yourself there is no real full stack and the application can continue to operate.

  2. The finally-block is not called in the debugger as the debugger closes immediately if there is an unhandled exception on the top level as there is no top-level exception handler. See this answer for a deeper explanation. If you run the application without debugger attached the finally-block will be called - thanks to bommelding for figuring that out.

  3. You use finally blocks each time you need to make sure to clean up correctly. See this answer for a deeper explanation.

  4. That's a tough question, I think this is meant to describe effects from PC settings like virus scanners terminating programs when they try to create a buffer overflow or a similar possibly critical situation. Similarly the execution of finally-blocks could be prevented by data execution prevention or other security features.

Community
  • 1
  • 1
Christoph Sonntag
  • 4,459
  • 1
  • 24
  • 49
0

1.Am i correctly understand that in try-catch-finally construction finally will always be executed? (Excluding Environment.FastFail())

The finally block will not be called after return in a couple of unique scenarios: if System.exit() is called first, or if the VM crashes.

2.Why finally block is not called?(In the code below)?

Read Here: Can a finally block be interrupted/suspended?

  1. For which cases we generally use try-finally?

try{} finally{} should be used in cases where you cannot handle the exception, but are required to clean up resources.

Or It is always better that to show something and exit gracefully after some exceptional exception has happened in our program.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
0

1 - It depends. The idea of using a finally block is to clean up resources allocated in the try block. Even if an exception occurs in the try block you can run code in the finally block. However, the finally block are guaranteed to run within a handled exception. If the exception is unhandled, execution of finally are dependent on the how the exception unwind operation is triggered.

2 - Your finally are not called since the exception is unhandled. Microsoft documentation states "Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements in a finally block that must be run even in that situation, one solution is to add a catch block to the try-finally statement." Compare the examples at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-finally to see difference between handled exception and non-handled exception.

Read more at this question Why use finally in C#?

Sources https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-finally https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch-finally

iikkoo
  • 2,810
  • 6
  • 33
  • 38