If I have the following working code:
List<MyClass> results = new List<MyClass>();
for(int i = 0; i < source.Count; i++)
try{ results.Add(Fetch(source[i])); }
catch(Exception ex){ results.Add(new MyClass("poof!")); }
...
public MyClass Fetch(Object source){ ... }
and would like to remake it into LINQ, how can I manage the try-catch? If I put it around the following expression, It's not going to poof me. My suggestion would be to migrate catching into the method but I'm not sure how optimal/recommended that is. Can one tell at all or is depending on specifics of the scenario?
IEnumerable<String> strings = source.Select(e => Fetch(e));
public MyClass Fetch(Object source){ try{ ... } catch(){ ... } }
It'd be cool with something like this pseudo-code. I'm thinking the method .First()
and it's more-potent brother .FirstOrDefault()
, but with additional capacity.
IEnumerable<String> strings = source.Select(e => Fetch(e)).Excepted(...);
Am I way off?