-
-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Propagate exceptions in lambda expressions passed into and invoked by methods.
An example is the LINQ methods.
There is always the case when lambdas are directly passed as a parameter but not at all be called as a consequence of the method.
But let's assume we want the exceptions of the lambda to be attached to the method.
This could be dealt with with some analysis using a special attribute. Intended for API designers.
Problem
// "Equals" might throw "ArgumentException"
_ = list.Any(x => x.Equals(2));// Should normally propagate exception. But not in this case, of course.
_ = list.Any([Throws(typeof(ArgumentException))] (x) => x.Equals(2));Solution proposal
What if you could do this?
// Would propagate the exceptions through "Any".
_ = list.Any([PropagateExceptions](x) => x.Equals(2));To be dealt with like so:
try
{
_ = list.Any([PropagateExceptions](x) => x.Equals(2));
}
catch (ArgumentException)
{
}And perhaps this for APIs:
public static bool Any<T>(this IEnumerable<T> source, [PropagateExceptions] Func<T, bool> predicate);Then the developer wouldn't have to add the attribute themselves.
Caveats
This would not work on properties.
Questions
Should the sites in the lambda still be highlighted? I think so.
But the diagnostics should be gone if handled outside.
Note
Of course, you should always handle exceptions close the source.