A finally block runs after the try/catch path finishes.

finally A `finally` block runs for both successful and handled failing paths.

Finally Clause

mode
FinallyClause.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static void Main()
    {
        string mode = "ok";
        bool closed = false;
        string status = "started";

        try
        {
            if (mode == "fail")
            {
                throw new InvalidOperationException("demo");
            }

            status = "finished";
        }
        catch (InvalidOperationException)
        {
            status = "handled";
        }
        finally
        {
            closed = true;
        }

        Console.WriteLine($"mode={mode}");
        Console.WriteLine($"status={status}");
        Console.WriteLine($"closed={closed}");
    }
}
using System;

class Program
{
    static void Main()
    {
        string mode = "fail";
        bool closed = false;
        string status = "started";

        try
        {
            if (mode == "fail")
            {
                throw new InvalidOperationException("demo");
            }

            status = "finished";
        }
        catch (InvalidOperationException)
        {
            status = "handled";
        }
        finally
        {
            closed = true;
        }

        Console.WriteLine($"mode={mode}");
        Console.WriteLine($"status={status}");
        Console.WriteLine($"closed={closed}");
    }
}
using System;

class Program
{
    static void Main()
    {
        string mode = "skip";
        bool closed = false;
        string status = "started";

        try
        {
            if (mode == "fail")
            {
                throw new InvalidOperationException("demo");
            }

            status = "finished";
        }
        catch (InvalidOperationException)
        {
            status = "handled";
        }
        finally
        {
            closed = true;
        }

        Console.WriteLine($"mode={mode}");
        Console.WriteLine($"status={status}");
        Console.WriteLine($"closed={closed}");
    }
}
  1. mode ← ok, closed ← False, status ← started

    4{5    static void Main()6    {7        string mode→ ok = "ok"; //@mode="fail", "skip"8        bool closed→ False = false;9        string status→ started = "started";
  2. status ← finished

    11try12{13    if (mode == "fail")14    {15        throw new InvalidOperationException("demo");16    }1718    status→ finished = "finished";19}
  3. closed ← True

    23}24finally25{26    closed→ True = true;27}
  4. Console.WriteLine($"mode={mode}");

    29    Console.WriteLine($"mode={modeok}");30    Console.WriteLine($"status={statusfinished}");31    Console.WriteLine($"closed={closedTrue}");32}
    outputmode=ok
    status=finished
    closed=True
  1. mode ← fail, closed ← False, status ← started

    4{5    static void Main()6    {7        string mode→ fail = "fail";8        bool closed→ False = false;9        string status→ started = "started";
  2. if (mode == "fail")

    12{13    if (modefail == "fail")14    {15        throw new InvalidOperationException("demo");16    }
  3. status ← handled

    19}20catch (InvalidOperationException)21{22    status→ handled = "handled";23}
  4. closed ← True

    23}24finally25{26    closed→ True = true;27}
  5. Console.WriteLine($"mode={mode}");

    29    Console.WriteLine($"mode={modefail}");30    Console.WriteLine($"status={statushandled}");31    Console.WriteLine($"closed={closedTrue}");32}
    outputmode=fail
    status=handled
    closed=True
  1. mode ← skip, closed ← False, status ← started

    4{5    static void Main()6    {7        string mode→ skip = "skip";8        bool closed→ False = false;9        string status→ started = "started";
  2. status ← finished

    11try12{13    if (mode == "fail")14    {15        throw new InvalidOperationException("demo");16    }1718    status→ finished = "finished";19}
  3. closed ← True

    23}24finally25{26    closed→ True = true;27}
  4. Console.WriteLine($"mode={mode}");

    29    Console.WriteLine($"mode={modeskip}");30    Console.WriteLine($"status={statusfinished}");31    Console.WriteLine($"closed={closedTrue}");32}
    outputmode=skip
    status=finished
    closed=True