try/catch Blocks
To prevent your code from totally bombing out, use try
/catch
blocks that can handle problems inside your code. If JavaScript encounters an error when executing code in a try
block, it will jump down and execute the catch
portion instead of stopping the entire script. If no error occurs, the whole try
block will be executed, and none of the catch
block will be executed.
For example, the following try
/catch
block tries to assign variable x
to a value of an undefined variable named badVarName
:
try{ var x = badVarName;} catch (err){ console.log(err.name + ': "' + err.message + '" occurred when assigning x.');}
Notice that the catch
statement accepts an err
parameter, which is an error object. ...
Get Learning AngularJS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.