不需要.例外情况需要在某处处理.如果您希望转换器中发生异常(对于应用程序而言这是正常的),您必须在转换器中有一个try-catch(以下代码示例将返回null失败的转换):
List<string> input = new List<string> { "1", "2", "three", "4" };
List<int?> converted = input.ConvertAll(s =>
{
int? result = null;
try
{
result = int.Parse(s);
}
catch (Exception) { }
return result;
});
Run Code Online (Sandbox Code Playgroud)
(是的,我知道我应该使用int.TryParse,但这不会引发异常...)
然而,吃这样的异常总会给出一种变通方法的气味,而且我想在我的代码中找不到任何东西.