Here is a piece of code:
IUser user = managerUser.GetUserById(UserId); if ( user==null ) throw new Exception(...); Quote quote = new Quote(user.FullName, user.Email);
Everything is fine here. But if I replace the string "if" with the following:
ComponentException<MyUserManagerException>.FailIfTrue(user == null, "Can't find user with Id=" + UserId);
where the implementation of the function is as follows:
public abstract class ComponentException<T> : ComponentException where T : ComponentException, new() { public static void FailIfTrue(bool expression, string message) { if (expression) { T t = new T(); t.SetErrorMessage(message); throw t; } }
Then ReSharper generates a warning to me: perhaps a "System.NullReferenceException" indicating the 1st use of the "user" object.
Q1. Why does this raise such an exception? As far as I see, if user==null exception will be thrown and execution will never reach the point of use.
Q2. How to remove this warning? Please note: 1. I do not want to suppress this warning with comments (I will have many similar parts and I do not want to turn my source code into 'commented garbage); 2. I do not want to change the ReSharper settings in order to change this problem from a warning to a โsuggestionโ in a โpromptโ.
Thank you
Any thoughts are welcome!
PS I am using Resharper 5.1, MVSV 2008, C #
Budda
source share