衡阳做网站,网站版面,wordpress图片墙插件,wordpress百度云直链咨询区 Michael Stum#xff1a;在项目开发中当抛出异常时#xff0c;我会简单的用 System.Exception#xff0c;但这种会捕获所有的异常#xff0c;我不希望大一统#xff0c;我只想捕获我预知的几个异常#xff0c;然后在这里处理一些特定的业务逻辑。目前我只能这么实… 咨询区 Michael Stum在项目开发中当抛出异常时我会简单的用 System.Exception但这种会捕获所有的异常我不希望大一统我只想捕获我预知的几个异常然后在这里处理一些特定的业务逻辑。目前我只能这么实现代码如下try
{WebId new Guid(queryString[web]);
}
catch (FormatException)
{WebId Guid.Empty;
}
catch (OverflowException)
{WebId Guid.Empty;
}上面的两种异常的处理逻辑是一致的我但重复写了 WebId Guid.Empty; 是否有办法可以只写一次呢回答区 Tamir Vered最简单的方法就是在 catch 作用域中使用 if 语句, 但在 C#6.0 之后就不需要这么麻烦了可以直接使用新特性 异常过滤器 这种特性已经被 CLR 直接支持而不仅仅是 MSIL 上的一些小动作修改后的代码如下try
{WebId new Guid(queryString[web]);
}
catch (Exception exception) when (exception is FormatException || ex is OverflowException)
{WebId Guid.Empty;
}上面的代码仅仅会捕获 InvalidDataException 和 ArgumentNullException 异常当然你可以在 when 子句中弄出更复杂的语句比如下面代码static int a 8;...catch (Exception exception) when (exception is InvalidDataException a 8)
{Console.WriteLine(Catch);
}值得注意的是Exception Filters 和 catch 中写 if 有着不同的语义当第一个 Exception Filters 中的判断条件不满足或者在内部抛出了异常代码会继续判断下一个 Exception Filters 参考代码如下。static int a 7;
static int b 0;...try
{throw new InvalidDataException();
}
catch (Exception exception) when (exception is InvalidDataException a / b 2)
{Console.WriteLine(Catch);
}
catch (Exception exception) when (exception is InvalidDataException || exception is ArgumentException)
{Console.WriteLine(General catch);
}Output: General catch如果 Exception Filter 有多个 true那么只会命中第一个。static int a 8;
static int b 4;...try
{throw new InvalidDataException();
}
catch (Exception exception) when (exception is InvalidDataException a / b 2)
{Console.WriteLine(Catch);
}
catch (Exception exception) when (exception is InvalidDataException || exception is ArgumentException)
{Console.WriteLine(General catch);
}Output: Catch.如果你观察代码的 IL你会发现Exception Filter 并不是多 if 判断的语法糖而是 MSIL 专门提供的 filter, endfilter 关键词法。点评区 现代化的 C# 语法糖太多了Michael Stum 大佬总结的很全面学习了。