Resharper 给出警告
Possible 'null' assignment to entity with '[NotNull]' attribute
为线
var strmR = new StreamReader(replyStream);
在下面的代码中(删除了详细的异常处理)。
Stream replyStream;
string reply;
var query = _serverBaseUrl + queryText;
var wreq = WebRequest.Create(query);
wreq.Timeout = _serverTimeoutLimit;
try
{
replyStream = wreq.GetResponse().GetResponseStream();
var strmR = new StreamReader(replyStream);
reply = strmR.ReadLine();
}
catch (WebException webex)
{
switch (webex.Status)
{
/*throw new exceptions*/
}
}
replyStream.Close();
return reply;
我的想法是,我不需要检查 replyStream 是否为 null,因为 WebException(或未处理的异常)应该已经被抛出(然后我会处理它并抛出新的自定义异常)如果发生任何错误。
replyStream 是否可能在没有抛出 WebException 的情况下为 null?
请您参考如下方法:
replyStream 不能为 null。如果 GetResponse() 成功,则 GetResponseStream 始终有一个值。如果出现任何问题,将抛出一个 WebException。 documentation of GetResponse()说了以下内容
If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.
因此,您也可以将 catch 子句扩展到句柄 webex.Response。
