Rory McCrossan
Rory McCrossan

Reputation: 337713

Resharper states if/else condition will never be hit, why?

I have this code from a PushSharp example which details how to handle exceptions. For some reason all the else conditions are greyed out by Resharper, stating that The expression is always false. I don't see how this is possible.

// ex is an Exception passed in to the method
if (ex is NotificationException)
{
    // Deal with the failed notification
    var notification = ((NotificationException)ex).Notification;
    var logItem = new PushLog($"{typePrefix} Notification failed", $"Notification Failed: {notification}");
    _pushLogRepo.Insert(logItem);
}
else if (ex is DeviceSubscriptionExpiredException) // Resharper says this is always false
{
   // exception handling code...
}
else if (ex is RetryAfterException) // Resharper says this is always false
{
   // exception handling code...
}
else
{
    Console.WriteLine("Notification Failed for some (Unknown Reason)");
}

Can someone explain how this is possible? I don't see how it can be. Here's a screenshot from VS2015 which is a bit clearer with syntax highlighting - ignore the error, I'm in the middle of refactoring.

enter image description here

Upvotes: 0

Views: 72

Answers (1)

SLaks
SLaks

Reputation: 888177

This would happen if those classes inherit NotificationException, since then the first branch would always hit.

Upvotes: 8

Related Questions