Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -72,6 +64,8 @@ public static int SingleCatchSwitchCase(string numeratorParam, string denominato
case OverflowException:
Console.WriteLine("Overflow Exception!");
break;
default:
throw;
}
}

Expand Down Expand Up @@ -102,5 +96,39 @@ public static int SingleCatchSwitchPattern(string numeratorParam, string denomin

return -1;
}

public static List<string> FilterRunsBeforeUnwinding()
{
var steps = new List<string>();

try
{
ThrowFromInnerMethod(steps);
}
catch (FormatException) when (Record(steps, "filter"))
{
steps.Add("catch");
}

return steps;
}

private static void ThrowFromInnerMethod(List<string> steps)
{
try
{
throw new FormatException();
}
finally
{
steps.Add("inner finally");
}
}

private static bool Record(List<string> steps, string step)
{
steps.Add(step);
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CatchMultipleExceptionsAtOnes\CatchMultipleExceptions.csproj" />
<ProjectReference Include="..\CatchMultipleExceptions\CatchMultipleExceptions.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Loading