android
android

Reputation: 652

Catch block does not catch exception

I have an action which should return an image if it was found on the server. If filename is valid, everything works as expected. But if the file was not found, it always throws an unhandled exception:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Why is this exception not handled by the catch block?

This is my simplified code:

public ActionResult Users(string filename)
    {
        try
        {
            var filePath = Server.MapPath("~/App_Data/uploads/users/" + filename);
            var file = File(filePath, "image/jpg", Path.GetFileName(filePath));
            return file;
        }
        catch
        {
            return Content("FILE NOT FOUND");
        }
    }

EDIT

Error:

Could not find file 'C:\Projects\Extranet\App_Data\uploads\users\test123.png'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Projects\Extranet\App_Data\uploads\users\test123.png'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stacktrace:

[FileNotFoundException: Could not find file 'C:\Projects\Extranet\App_Data\uploads\users\test123.png'.]
   System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +9726591
   System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) +1142
   System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +83
   System.Web.HttpResponse.TransmitFile(String filename, Int64 offset, Int64 length) +132
   System.Web.HttpResponseWrapper.TransmitFile(String filename) +20
   System.Web.Mvc.FilePathResult.WriteFile(HttpResponseBase response) +17
   System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +188
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8967885
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Upvotes: 2

Views: 1923

Answers (1)

Morten Mertner
Morten Mertner

Reputation: 9474

The file your are trying to retrieve does not exist, and so you get an IOException when MVC is processing your ActionResult.

This happens outside of your controller method and thus outside your try-catch block. Try the following code instead:

public ActionResult Users(string filename)
{
    var filePath = Server.MapPath("~/App_Data/uploads/users/" + filename);
    return File.Exists( filePath ) ? File(filePath, "image/jpg", Path.GetFileName(filePath)) : Content( "FILE NOT FOUND" );
}

Upvotes: 4

Related Questions