开发者

ASP.NET MVC 3 controllers throwing exceptions even though I have a catch?

开发者 https://www.devze.com 2023-03-24 17:37 出处:网络
In my ASP.NET MVC 3 app, I have the following code: try { return new ProxyResult(new Uri(\"http://example.org\"));

In my ASP.NET MVC 3 app, I have the following code:

try
{
    return new ProxyResult(new Uri("http://example.org"));
}
catch (WebException)
{
}

However, when I try to access the page, I get a WebException (404 Not Fou开发者_如何学Cnd)... but the WebException should be caught. I don't understand how this can possibly jump right out of my catch.


All you are putting in your try block is a simple constructor call to the ProxyResult which by the way is not some standard result => it's probably something custom. The actual execution of the result (the invoke of the ExecuteResult) method which might potentially throw the exception you are expecting happens much later and outside of your controller action. That's the reason why no exception is throw in your controller action. You should put the try/catch inside the ExecuteResult method of this custom ProxyResult class that you have written.


The proxy result isn't executed until after the action method is returned. All you are doing is returning an instruction for MVC to process AFTER it has returned.

Use the HandleError attribute to handle exceptions:

http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消