I came across a bug in my code today that would cause the following exception to be thrown when I had duplicate identifiers in my list:
System.InvalidOperationException: Sequence contains more than one matching element
While I know I can catch this exception like so:
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;
}
..but this will also catch all other InvalidOperation
exceptions, including Sequence contains no elements
, Sequence contains no matching element
and many others.
How can I make it so that I'm only catching the Sequence contains more than one matching element
exception?
I want to only catch this specific exception message because I want to leave feedback in the log file of my application explaining which part of the configuration is wrong.