locked
StorageFolder.getFileAsync() throws exception when file doesn't exist in Win8 RP.

    Question

  • I've just migrated my Consumer Preview app to Win8 RP.

    In the consumer preview StorageFolder.getFileAsync() would simply execute the completion handler with no arguments when a file was not found.

    This would work:

    dir.getFileAsync(filename).then(function (file) {
    	if (!file) {
    		//file not found
    	}
    }, function(error){
    	//error handling
    });

    In the RP this doesn't work. It throws an exception at some dynamic WinJS code:

    function getResultsOfAsyncOp(op) {
        return op.getResults();
    }

    0x80070002 - JavaScript runtime error: The system cannot find the file specified.

    It does not call my compete handler, nor does it call the error handler.

    How am I supposed to check if a file exists in Win8 RP?

    Monday, June 4, 2012 5:45 AM

Answers

  • I think you may be seeing the exception here because of a change in the defaults in the Visual Studio debugger.  The previous version did not break on JavaScript runtime exceptions by default.  You had to enable this behavior in the DEBUG > Exceptions menu.

    If you continue the debugger from the point of exception, it should hit your error handler (if I understand it correctly).

    There are at least a couple of ways to work this.

    1. Set the debugger to ignore File Not Found Runtime exception either in the DEBUG > Exceptions menu or the checkbox on the exceptions dialog you get when the exception notifies you.  You do this because you do have a handler in place for the error.
    2. Use the try/catch pattern around the file access.

    Monday, June 4, 2012 10:19 AM

All replies

  • I think you may be seeing the exception here because of a change in the defaults in the Visual Studio debugger.  The previous version did not break on JavaScript runtime exceptions by default.  You had to enable this behavior in the DEBUG > Exceptions menu.

    If you continue the debugger from the point of exception, it should hit your error handler (if I understand it correctly).

    There are at least a couple of ways to work this.

    1. Set the debugger to ignore File Not Found Runtime exception either in the DEBUG > Exceptions menu or the checkbox on the exceptions dialog you get when the exception notifies you.  You do this because you do have a handler in place for the error.
    2. Use the try/catch pattern around the file access.

    Monday, June 4, 2012 10:19 AM
  • Your second option makes no sense to me. The promise is supposed to handle the exception and hand it into the promise error handler. This new debugger "feature" is annoying! Why change the default behaviour anyway? It was perfectly fine.

    On the other hand the VS chain of products is still riddled with bugs that prevent a "fast and fluid" development cycle.

    Monday, June 4, 2012 11:38 AM
  • Thanks for the reply. I see.

    About the second option:

    I don't understand this either.

    How am I supposed to use try/catch around an asynchronous call?


    Monday, June 4, 2012 11:52 PM
  • Not a fan of option 2 myself but it was suggested with some apparent success in this thread.
    Tuesday, June 5, 2012 12:31 AM