実際には「単純な」グローバル例外フィルターであるという事実によりFirstChanceException
、私は軌道に乗りました(それが「正しい」軌道であるかどうかはまだわかりません):
CLI にはすでに例外フィルターがあります。
C# 6 で作業する余裕がある場合は、次のように簡単です。
try
{
throw new NullReferenceException("No, Really");
}
catch(Exception ex) when (FilterExType(ex))
{
Console.WriteLine($"2: Caught 'any' exception: {ex}");
}
static bool FilterExType(Exception ex)
{
if (ex is NullReferenceException)
{
Environment.FailFast("BOOM from C#!", ex);
}
// always handle if we return
return true;
}
そして、以前のバージョンに固執している私たち (私のような) の場合、デリゲート/ラムダを介して VB.NET を介してフィルタリングをルーティングできます。
try {
VbFilterLib.FilteredRunner.RunFiltered(() =>
{
throw new NullReferenceException("Via VB.NET");
});
}
catch (Exception ex)
{
Console.WriteLine("1: Caught 'any' exception: {0}", ex");
}
VBをそのまま使用します(ご容赦ください。VB.NETは私が堪能な言語とはほど遠いです):
Public Class FilteredRunner
Delegate Sub VoidCode()
Private Shared Function FilterAction(x As Exception) As Boolean
If TypeOf x Is NullReferenceException Then
Environment.FailFast("Abort program! Investigate Bug via crash dump!", x)
End If
' Never handle here:'
Return False
End Function
Public Shared Sub RunFiltered(code As VoidCode)
Try
code.Invoke()
Catch ex As Exception When FilterAction(ex)
Throw New InvalidProgramException("Unreachable!", ex)
End Try
End Sub
End Class
明らかに、それを機能させるには、さらに構成のリギングが必要ですが、それはまさに私が望んでいることのようです. :-)