Exceptions and Defensive Coding
Finally Clause
A finally block runs after the try/catch path finishes.
finally
A `finally` block runs for both successful and handled failing paths.
Finally Clause
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}");
}
}
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";status ← finished
11try12{13 if (mode == "fail")14 {15 throw new InvalidOperationException("demo");16 }1718 status→ finished = "finished";19}closed ← True
23}24finally25{26 closed→ True = true;27}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
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";if (mode == "fail")
12{13 if (modefail == "fail")14 {15 throw new InvalidOperationException("demo");16 }status ← handled
19}20catch (InvalidOperationException)21{22 status→ handled = "handled";23}closed ← True
23}24finally25{26 closed→ True = true;27}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
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";status ← finished
11try12{13 if (mode == "fail")14 {15 throw new InvalidOperationException("demo");16 }1718 status→ finished = "finished";19}closed ← True
23}24finally25{26 closed→ True = true;27}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