Try Catch

You should always include a catch statement with the appropriate await in front of a promise.
You should always place the catch statement outside of the Excel.run to ensure that all the errors will be caught.


If you use console.log() to print error messages to the console, those messages will only be visible on the server.
End-users will not see those error messages in the add-in taskpane or anywhere in the host application.


Exceptions are thrown using the throw statement
The throw object can be anything


if (anError) { 
throw {
name: "title",
message: "description"
};

try-catch-throw 
try {
throw {
name: "title",
message: "description"
};
} catch (e) {
// handle exception
} finally {
//clean up
}

msdn - mt616487


function myFunction () { 
Excel.run ( function (context) {
// some code

}.catch( function (error) {
console.log("Error : " + error);
      if (error.instanceof OfficeExtension.Error) {
         console.log("Debug info : " + JSON.stringify(error.debugInfo) );
}
});


Passing an invalid coercion type

As mentioned before, the callback function exposes a result, so we can attach additional behavior to the button to display a status message.

Office.context.document.setSelectedDataAsync(message, 
{ coercionType: Office.CoercionType.Ooxml },
messageCallback);

If a user attempts to perform the now invalid action, instead of simply failing, they're presented with a helpful error message letting them know something went wrong.


if (asyncResult === Office.AsyncResultStatus.Failed) { 
var error = asyncResult.error;
app.showNotification(error.code + " : " + error.name, error.message);
}


function getHostInformation() { 
var hostInfoValue = session.storage.getItem('hostInfoValue');
var items = hostInfoValue.split('$');
if (!items[2]){
items = hostInfoValue.split('|');
}

var hostInfo = {
type: items[0],
platform: items[1],
version: items[2],
culture: items[3]
}
return hostInfo;
}


© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext