diff --git a/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchExamples.cs b/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchExamples.cs index be6ebf2bea..b8e888a1b4 100644 --- a/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchExamples.cs +++ b/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchExamples.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CatchMultipleExceptionsAtOnes +namespace CatchMultipleExceptionsAtOnes { public class CatchExamples { @@ -42,9 +36,7 @@ public static int SingleCatchWithWhen(string numeratorParam, string denominatorP return (int)(numerator / denominator); } - catch (Exception ex) when (ex is FormatException || - ex is DivideByZeroException || - ex is OverflowException) + catch (Exception ex) when (ex is FormatException or DivideByZeroException or OverflowException) { return -1; } @@ -72,6 +64,8 @@ public static int SingleCatchSwitchCase(string numeratorParam, string denominato case OverflowException: Console.WriteLine("Overflow Exception!"); break; + default: + throw; } } @@ -102,5 +96,39 @@ public static int SingleCatchSwitchPattern(string numeratorParam, string denomin return -1; } + + public static List FilterRunsBeforeUnwinding() + { + var steps = new List(); + + try + { + ThrowFromInnerMethod(steps); + } + catch (FormatException) when (Record(steps, "filter")) + { + steps.Add("catch"); + } + + return steps; + } + + private static void ThrowFromInnerMethod(List steps) + { + try + { + throw new FormatException(); + } + finally + { + steps.Add("inner finally"); + } + } + + private static bool Record(List steps, string step) + { + steps.Add(step); + return true; + } } } diff --git a/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchMultipleExceptions.csproj b/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchMultipleExceptions.csproj index 74abf5c976..dfb40caafc 100644 --- a/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchMultipleExceptions.csproj +++ b/exception-handling/CatchMultipleExceptions/CatchMultipleExceptions/CatchMultipleExceptions.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net10.0 enable enable diff --git a/exception-handling/CatchMultipleExceptions/TestProject/TestProject.csproj b/exception-handling/CatchMultipleExceptions/TestProject/TestProject.csproj index b902dc2361..99dbfc6840 100644 --- a/exception-handling/CatchMultipleExceptions/TestProject/TestProject.csproj +++ b/exception-handling/CatchMultipleExceptions/TestProject/TestProject.csproj @@ -1,21 +1,21 @@ - net6.0 + net10.0 enable false - - - - + + + + - + diff --git a/exception-handling/CatchMultipleExceptions/TestProject/TryCatchUnitTest.cs b/exception-handling/CatchMultipleExceptions/TestProject/TryCatchUnitTest.cs index ccd034b4fa..f10155b803 100644 --- a/exception-handling/CatchMultipleExceptions/TestProject/TryCatchUnitTest.cs +++ b/exception-handling/CatchMultipleExceptions/TestProject/TryCatchUnitTest.cs @@ -59,5 +59,13 @@ public void WhenOverflowError_ThenInCorrectOutput() Assert.IsTrue(result1 == -1 && result2 == -1 && result3 == -1 && result4 == -1); } + + [TestMethod] + public void WhenFilterMatches_ThenFilterRunsBeforeInnerFinally() + { + var steps = CatchExamples.FilterRunsBeforeUnwinding(); + + CollectionAssert.AreEqual(new[] { "filter", "inner finally", "catch" }, steps); + } } } \ No newline at end of file