I'm facing a weird issue with catch clause filtered:
catch (System.Net.Http.HttpRequestException ex)
{
ManagerHelper.Current.ExceptionHandler.Log($"random message",
log4net.Core.Level.Error, ex: ex);
}
catch (ArgumentNullException ex)
{
ManagerHelper.Current.ExceptionHandler.Log($"random message for null value",
log4net.Core.Level.Error, ex: ex);
}
catch (Exception ex) when (!(ex is System.Net.Http.HttpRequestException) && !(ex is ArgumentNullException))
{
ManagerHelper.Current.ExceptionHandler.Log($"random message for other
exception", log4net.Core.Level.Error, ex: ex);
}
Actually when i throw an HttpRequestException the expected behavior would be to enter into the catch of the specific error. For an unknow reason even if the when clause result is false i enter actually into the catch (Exception ex) and not into the catch (System.Net.Http.HttpRequestException ex).
On the immediate window:
!(ex is System.Net.Http.HttpRequestException) && !(ex is ArgumentNullException) => false
Do someone have an explanation of this?
Thanks in advance for your responses and/or advices.
Edited the sample to be more clear.
An other try i did:
catch (Exception ex)
{
Type varType = ex.GetType();
if (ex is System.Net.Http.HttpRequestException)
ManagerHelper.Current.ExceptionHandler.Log($"random message", log4net.Core.Level.Error, ex: ex as
System.Net.Http.HttpRequestException);
else if (ex is ArgumentNullException)
ManagerHelper.Current.ExceptionHandler.Log($"random message for null value", log4net.Core.Level.Error, ex: ex as
ArgumentNullException);
else
ManagerHelper.Current.ExceptionHandler.Log($"random message for other value", log4net.Core.Level.Error, ex: ex);
}
the varType content is:
{Name = "HttpRequestException" FullName = "System.Net.Http.HttpRequestException"}
The first if result is false and the code is entering in the else statement in debug.
if i do:
varType == typeof(System.Net.Http.HttpRequestException)
The result is true.
when
filter is not needed. If one of the catch blocks rethrows the exception, this will bubble up to the next level of try/catch up the call stack, and will not be handled by further catch blocks in the same try/catch level.when
clause actually excludes HttpRequestException (due to the!
). so only the final catch will run.