c# - 如何捕获特定类型的 InvalidOperationException?
问题描述
我今天在我的代码中遇到了一个错误,当我的列表中有重复的标识符时,它会导致引发以下异常:
System.InvalidOperationException: Sequence contains more than one matching element
虽然我知道我可以像这样捕获这个异常:
string id = "foo";
List<string> DirtyList = new List<string>{"foo","bar","xyzzy","foo"};
string result = null;
try{
result = DirtyList.Single(x=>x.Equals(id));
}
catch( InvalidOperationException e )
{
Console.WriteLine( e );
throw;
}
..但这也将捕获所有其他InvalidOperation
异常,包括Sequence contains no elements
和Sequence contains no matching element
许多其他异常。
我怎样才能使它只捕获Sequence contains more than one matching element
异常?
我只想捕获这个特定的异常消息,因为我想在我的应用程序的日志文件中留下反馈,解释配置的哪一部分是错误的。
解决方案
推荐阅读
- php - Laravel 文件系统重启
- android - Android JNI - 如何从共享对象导入函数
- ios - Swift 4 - 如何在 pod 类中导入 AppDelegate
- php - 在 Woocommerce 的特定产品单页中隐藏相关产品
- javascript - 为什么fancybox幻灯片停止了?
- ruby-on-rails - 保持 Rails 服务器运行的 Cron 作业
- c++ - 模板与常规功能的歧义 - UB?
- python - 使用 googlemaps api,places_nearby 用某个关键字查找附近的位置
- r - 卡在数据操作上
- spring - 使用 RepositoryItemReader nativeQuery 的 spring 批处理