Здравствуйте, Тепляков Сергей Владимирович, Вы писали:
ТСВ>Аннотация:
ТСВ>Данная статья рассказывает о новой возможности C# 6.0 – фильтрах исключений. Приводятся примеры применения, разбираются опасности и проблемы их применения.
Самые важные на практике нюансы не раскрыты, неплохо бы добавить в статью
1.
X DO NOT throw exceptions from exception filter blocks.
When an exception filter raises an exception, the exception is caught by the CLR, and the filter returns false. This behavior is indistinguishable from the filter executing and returning false explicitly and is therefore very difficult to debug.
(
пруф)
У нас этот момент года три назад больно выстрелил в продакшне буквально через месяц после добавления.
2. Фильтр выполняется _перед_ _всеми_ finally выше по стеку,
пруф #2. Оно как бы очевидно, но не менее больно выстреливает. Для сомневающихся: попробуйте угадать вывод в
static bool Filter(string message, bool filter)
{
Console.WriteLine(message);
return filter;
}
static void A()
{
try
{
B();
}
catch when (Filter("A filter", true))
{
Console.WriteLine("A catch");
}
finally
{
Console.WriteLine("A finally");
}
}
static void B()
{
try
{
C();
}
catch when (Filter("B filter", false))
{
Console.WriteLine("B catch");
throw;
}
finally
{
Console.WriteLine("B finally");
}
}
static void C()
{
try
{
throw null;
}
catch when (Filter("C filter", true))
{
Console.WriteLine("C catch");
throw;
}
finally
{
Console.WriteLine("C finally");
}
}