Answered by:
Handling exception with getFileAsync

Question
-
Hi,
I'm trying to open a file called myFile.data as follows. However, if myFile.data doesn't exist it crashes and I can't figure out how to avoid this. How can I check for the existence of this file or else handle the exception (note that it never writes out 'Here 3').
Thanks in advance, S
console.log("Here 1"); var localFolder = Windows.Storage.ApplicationData.current.localFolder; console.log("Here 2"); localFolder.getFileAsync("myfile.data") .then(function (file) { console.log("Here 3"); return Windows.Storage.FileIO.readTextAsync(file); }).done(function (text) { var activitiesArray = JSON.parse(text); var activitiesList = new WinJS.Binding.List(activitiesArray); var publicMembers = { itemList: activitiesList }; WinJS.Namespace.define("DataExample", publicMembers); });
Saturday, October 27, 2012 11:26 PM
Answers
-
When you define the promise pass an error function in addition to the completion function. See How to handle errors with promises for details.
--Rob
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Wednesday, October 31, 2012 8:43 PM
Sunday, October 28, 2012 1:01 AMModerator -
Got it, here is what I did in case it helps others:
var localFolder = Windows.Storage.ApplicationData.current.localFolder; localFolder.getFileAsync("myfile.data") .then(function (file) { return Windows.Storage.FileIO.readTextAsync(file); }, function (error) { console.log("Problem reading myfile.data"); createDataModel(); }).done(function (text) { try { var activitiesArray = JSON.parse(text); var activitiesList = new WinJS.Binding.List(activitiesArray); var publicMembers = { itemList: activitiesList }; WinJS.Namespace.define("DataExample", publicMembers); } catch (error) { console.log("Can't read file"); } });
- Marked as answer by Song Tian Monday, October 29, 2012 7:25 AM
Sunday, October 28, 2012 1:31 AM
All replies
-
you can try the
try { // your code here } catch(e) { // if there's an exception thrown handle it here }
approach.
PS: you risk getting an error for defining the namespace like that, meaning it's not always defined (when the file doesn't exists) and you supposedly make the presumption that it's always defined and ready to use in another part of your app.
Sunday, October 28, 2012 12:48 AM -
Hi,
Thanks but this unfortunately doesn't work as its an asynchronous call so its past the catch before it crashes!
Sunday, October 28, 2012 12:58 AM -
When you define the promise pass an error function in addition to the completion function. See How to handle errors with promises for details.
--Rob
- Marked as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Wednesday, October 31, 2012 8:43 PM
Sunday, October 28, 2012 1:01 AMModerator -
Got it, here is what I did in case it helps others:
var localFolder = Windows.Storage.ApplicationData.current.localFolder; localFolder.getFileAsync("myfile.data") .then(function (file) { return Windows.Storage.FileIO.readTextAsync(file); }, function (error) { console.log("Problem reading myfile.data"); createDataModel(); }).done(function (text) { try { var activitiesArray = JSON.parse(text); var activitiesList = new WinJS.Binding.List(activitiesArray); var publicMembers = { itemList: activitiesList }; WinJS.Namespace.define("DataExample", publicMembers); } catch (error) { console.log("Can't read file"); } });
- Marked as answer by Song Tian Monday, October 29, 2012 7:25 AM
Sunday, October 28, 2012 1:31 AM