Answered by:
Why "fail" in promise does not catch errors?

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?
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)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, May 16, 2012 1:21 PM
- Marked as answer by Dino He Tuesday, May 22, 2012 7:11 AM
Wednesday, May 16, 2012 1:21 PMModerator
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.
- Edited by jrboddie Monday, May 14, 2012 11:54 AM
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Monday, May 14, 2012 12:27 PM
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..
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)
- Proposed as answer by Jeff SandersMicrosoft employee, Moderator Wednesday, May 16, 2012 1:21 PM
- Marked as answer by Dino He Tuesday, May 22, 2012 7:11 AM
Wednesday, May 16, 2012 1:21 PMModerator