locked
Why "fail" in promise does not catch errors? RRS feed

  • Question

  • I'm trying to access a file, that might not exist:

    var localFolder = Windows.Storage.ApplicationData.current.localFolder;
        	
    localFolder.getFileAsync(stateFile).then(function (file) {
        		Windows.Storage.FileIO.readTextAsync(file).then(function (text) {
    		// do something with the text
    
    	});
    }, function (err) {
    	// log error, load dummy data
    
    });

    if the file is not there, the "fail" method does get called, BUT it happens only AFTER my application halts with an exception "file not found". only when I press "continue" for the debugger does it continue to the "fail" method..

    what am i doing wrong? should i check for existence beforehand?


    http://blogs.microsoft.co.il/blogs/eladkatz @ElatKt

    Monday, May 14, 2012 7:12 AM

Answers

  • Elad,

    There are two mechanisms to indicate failure in Javascript.  Your promise can fail with an error and that will end up in the error function.  An exception can be thrown.  If an exception is thrown then you can inspect that exception for more information as to why it failed in a catch.  Using a try catch block is always a good idea for file operations.

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, May 16, 2012 1:21 PM
    Moderator

All replies

  • Do you have Menu Debug > Exceptions... > JavaScript Runtime Exceptions checked as Thrown?

    If so, this is normal behavior for debugging purposes.  Try turning it off--that is, making it User-handled.




    Monday, May 14, 2012 11:53 AM
  • Thank you for your answer.

    Are you saying that this is how it supposed to be? I'm supposed to use the "fail" to catch "file not found" errors, and during development i can either see those errors nonetheless or ignore them all altogether? it sounds highly unlikely to me...

    for now i just wrapped everything with a "try-catch" block, but it works, but still feels not right to me..


    http://blogs.microsoft.co.il/blogs/eladkatz @ElatKt

    Tuesday, May 15, 2012 8:55 PM
  • That is exactly right.  You can also choose to make the debugger ignore specific types of errors in the Debugger Exception Settings Panel.


    • Edited by jrboddie Tuesday, May 15, 2012 10:36 PM
    Tuesday, May 15, 2012 10:35 PM
  • Elad,

    There are two mechanisms to indicate failure in Javascript.  Your promise can fail with an error and that will end up in the error function.  An exception can be thrown.  If an exception is thrown then you can inspect that exception for more information as to why it failed in a catch.  Using a try catch block is always a good idea for file operations.

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, May 16, 2012 1:21 PM
    Moderator