Considering that JS errors are much more likely to be generated than the code on the server, and it is difficult to find and correct them, JS code must have exception handling and global processing. Errors are inevitable during the running of the program, and the running results after errors are often incorrect. Therefore, programs with errors during running are forcibly aborted. Running errors are collectively referred to as exceptions. in order to get a chance to handle errors, JavaScript provides exception handling statements. Include try-catch, try-catch-finally, and throw.
Try-catch statement
try{tryStatements}catch(exception){catchStatements}
Parameter description:
TryStatements: required. The sequence of statements that may cause errors.
Exception: required. Any variable name used to reference the error object when an error occurs.
CatchStatements: Optional. Error handling statement, used to handle errors in tryStatements.
During encoding, statements that may cause errors are often written into the curly brackets of the try block and the catch block after the try block is used to handle errors. The Error message is contained in an Error object, which can be accessed through reference of exception. Based on the error information in the error object, determine if the error is handled.