尝试捕捉不起作用

use*_*005 3 c# webclient exception try-catch

class XWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri Url)
    {
        var Request = base.GetWebRequest(Url);
        ...........
        return Request;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用:

try
{
    XWebClient Client = new XWebClient();
    Client.DownloadString(new Uri("badurl:100500"));
}
catch
{
    MessageBox.Show("exception");
}
Run Code Online (Sandbox Code Playgroud)

我希望得到一个消息框,但我得到一个未处理的异常.我究竟做错了什么?


例外:System.NotSupportedexception

消息:无法识别URI前缀

跟踪:

   System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)
   System.Net.WebRequest.Create(Uri requestUri)
   System.Net.WebClient.GetWebRequest(Uri address)
   XWebClient.GetWebRequest(Uri Url) ? [path]\XWebClient.cs:?????? 28
   System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
Run Code Online (Sandbox Code Playgroud)

Ed *_* S. 5

听起来像你在调试器下运行它并启用了"中断所有托管异常".如果是这种情况,调试器将在处理程序运行之前抛出异常的行中断.您的处理程序仍在工作,调试器只是让您事先检查错误.