ASP.NET MVC 3 controllers throwing exceptions even though I have a catch?
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
精彩评论