【发布时间】:2014-08-22 05:48:11
【问题描述】:
在浏览 MS 示例和 Stackoverlow 示例时,我发现了很多处理异常或不处理异常的示例t 处理异常。
一个例子,我觉得很有趣的是https://stackoverflow.com/a/137300/465292
using 内using
另一个例子(来自https://stackoverflow.com/a/3279967/465292)
var request = WebRequest.Create(requestUri) as HttpWebRequest;
if (request != null)
对我来说似乎是假的,因为文档中没有。 http://msdn.microsoft.com/en-us/library/bw00b1dc%28v=vs.110%29.aspx, WebRequest.Create() 返回null
Microsoft 自己的示例 http://msdn.microsoft.com/en-us/library/es54hw8e%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1,将 WebRequest 和 WebReponse 放在一个 try 块中。
【问题讨论】:
-
对于您的“另一种情况”:
WebRequest.Create("ftp://some.site") as HttpWebRequest确实会返回 null -“Create 方法返回 WebRequest 的后代”,但并非一直都需要HttpWebRequest。 -
@AlexeiLevenkov,好吧,如果 WebRequest.Create("address") 返回 null,则表示不支持方案。让
WebRequest.Create()抛出NotSupportedException异常不是更好吗?编辑:好吧,没必要……如果您请求 ftp 地址,并期望HttpWebRequest作为响应(as HttpWebRequest),那是的。null会是更好的选择!
标签: c# asp.net .net exception exception-handling