Why can I write a generic catch statement in C# that does nothing? [duplicate]
-
19-09-2019 - |
Question
Possible Duplicate:
Why can’t I catch a generic exception in C#?
I have been reviewing and writing Circuit Breaker code recently. The following method compiles, but the catch block is never entered. I have plenty of work-arounds, and this isn't the only way to get the right behavior (filtering exceptions), but I'm curious why this compiles and doesn't work!
public void AttemptCall<TException>(Action action)
where TException : Exception
{
try
{
action();
}
catch(TException e) // This block is never entered!
{
state.ActUponException(e);
throw;
}
}
Here is a test that should enter the catch block of the previous method.
[TestMethod]
public void Throw_an_exception()
{
circuitBreaker.AttemptCall<Exception>(() => throw new Exception());
// test the circuit breaker's state
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow