Why isn't the finally getting executed?
Asked Answered
B

5

6

It was my assumption that the finally block always gets executed as long as the program is running. However, in this console app, the finally block does not seem to get executed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new Exception();
            }
            finally
            {
                Console.WriteLine("finally");
            }
        }
    }
}

Output

Result

Note: When the exception was thrown, windows askmed me if I wanted to end the appliation, I said 'Yes.'

Bronson answered 10/1, 2013 at 16:35 Comment(5)
Click no and see what happensSapowith
run it from the command line instead of within your IDEElongation
It's look very strange. 'finally' should be wrote exactly after StackTrace.Illiteracy
there is no catch, the exception is unhandled and the app quitsChapple
Ilya - There is no option to hit no. The other option is to "debug". Kevin - it is running from the command line, not the IDE.Bronson
C
2

When you get the "ConsoleApplication1" has stopped responding, you have two choices.

Windows Error Reporting dialog

If you press cancel, the unhandled exception is allowed to continue until eventually the application is terminated. This allows the finally block to execute. If you do not press cancel then Windows Error Reporting halts the process, collects a minidump and then terminates the application. This means the finally block is not executed.

Alternatively, if you handle the exception in a higher method you will definitely see the finally block. For example:

static void unhandled()
{
    try
    {
        throw new Exception();
    }
    finally
    {
        Console.WriteLine("finally");
    }
}

static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
    try
    {
        unhandled();
    }
    catch ( Exception )
    {
        // squash it
    }
}

Always gives the output "finally"

Comanchean answered 10/1, 2013 at 17:24 Comment(0)
P
6

It executed actually. Just you didn't notice. Just when you see Windows is checking for a solution to the problem click Cancel and see it.

enter image description here

Portative answered 10/1, 2013 at 16:38 Comment(1)
What environment are you running in? I am using windows 7, 64. When it asks you "Windows is checking for a solution to the problem.." don't click cancel. Let it do its thing.Bronson
N
2

It's possible that you are debugging and when you click no, the execution is being halted by the debugger.

Nobelium answered 10/1, 2013 at 16:38 Comment(0)
C
2

When you get the "ConsoleApplication1" has stopped responding, you have two choices.

Windows Error Reporting dialog

If you press cancel, the unhandled exception is allowed to continue until eventually the application is terminated. This allows the finally block to execute. If you do not press cancel then Windows Error Reporting halts the process, collects a minidump and then terminates the application. This means the finally block is not executed.

Alternatively, if you handle the exception in a higher method you will definitely see the finally block. For example:

static void unhandled()
{
    try
    {
        throw new Exception();
    }
    finally
    {
        Console.WriteLine("finally");
    }
}

static void Main(string[] args)
{
    AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
    try
    {
        unhandled();
    }
    catch ( Exception )
    {
        // squash it
    }
}

Always gives the output "finally"

Comanchean answered 10/1, 2013 at 17:24 Comment(0)
S
1

When running from command line. Just when Windows tries to end application gracefully click no or cancel if application is not responding. enter image description here

Sapowith answered 10/1, 2013 at 16:39 Comment(2)
What environment are you running in? I am using windows 7, 64. When it asks you "Windows is checking for a solution to the problem.." don't click cancel. Let it do its thing.Bronson
Then Windows will terminate application without letting it to print to consoleSapowith
O
-2

An exception bubbles up the stack until it finds a handler. If it doesn't, the program exits. That's the case in your scenario... there's no handler, so the program exits before it hits the finally block.

Overbalance answered 10/1, 2013 at 16:40 Comment(3)
-1 – finally should be executed before bubbling up the stack – in fact, during the stack unwinding.Albertina
the entire purpose of finally apparently loses its use if there isnt a catch? thats a new one on me :LScrofula
Sorry, it was a thoughtless answer.Overbalance

© 2022 - 2024 — McMap. All rights reserved.