4

I assume I'm missing something really trivial here but for reason it's not obvious to me. I've always assumed that "finally" always executes, regardless of an exception or not.

Anyway, this code failed to run and I'm not sure why. It gets to i = i/j and throws an DivideByZero exception but I would've thought it would continue and execute the finally statement before stopping.

static void Main(string[] args)
{
    int i = 1;

    try
    {
        int j = 0;
        i = i / j;

        Console.WriteLine("can't get");
    }
    finally
    {
        Console.WriteLine("finally ran");
    }
}
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
Diskdrive
  • 18,107
  • 27
  • 101
  • 167
  • Strange!! I always thought that this code will end up with compile time error. I tried and it behaves exactly the way Jon Skeet has mentioned in his answer. – Pawan Mishra Nov 19 '11 at 07:36

5 Answers5

5

Take a look at this MSDN try-finally (C# Reference)

From above link:

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.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
4

Works for me - at least somewhat. When I run this as a console app from the command line, the "Test.exe has stopped working. Windows is looking for a solution" dialog comes up immediately, but then if I hit the "cancel" button, I see "finally ran". If I let the initial dialog run to completion and just get left with Debug or Close, then hitting Close will terminate the process immediately, and hitting Debug obviously brings up a debugger.

EDIT: Mark Hall's answer explains the behaviour in more detail. I'll leave this answer up as it contains experimental results, but look at Mark's answer :)

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • ok that's weird. I'm using Visual Studio 2010 Express to run this, it gets to the exception, and then i can't proceed any further. Maybe I've got to change my Exceptions handling settings... – Diskdrive Nov 19 '11 at 07:28
  • Ah but also, the "looking for a solution" thing I would've thought that's weird too. Cause I would've thought that it would just hit the exception and just keep going without prompting. Is that a behaviour of console apps? – Diskdrive Nov 19 '11 at 07:29
  • @stirfry: No, because you're not catching the exception. It indicates a failure in the program such that the program *should not continue*. – Jon Skeet Nov 19 '11 at 07:37
2

Mark's answer says what happens, but I thought I'd mention why:

I believe this is to allow for any external handlers to handler the exception to be handled before the finally block is executed. (For example, when a debugger is attached, it can try to break at the point of exception, allowing you to continue before the finally block starts running.)

If the finally block was executed beforehand, then you couldn't handle the exception in a debugger.

user541686
  • 205,094
  • 128
  • 528
  • 886
0

try this:


static void Main(string[] args)
     {
        int i = 1;

    try
    {
        int j = 0;
        i = i / j;

        Console.WriteLine("can't get");
    }
    catch(Exception ex){
        Console.WriteLine(ex.Message);
     }
    finally
    {
        Console.WriteLine("finally ran");
    }
    Console.WriteLine("Press enter to exit...");
    Console.ReadLine();
}

Prashant Lakhlani
  • 5,758
  • 5
  • 25
  • 39
0

I think you might need a catch to catch the exception before finally will run.

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101