Exception handling in JavaScript mainly uses the try catch finally statement and the onerror event of the window object to catch exceptions.
try catch finally can only catch runtime errors and cannot catch syntax errors. It can obtain error information, stack, error file, line number, and column number. The try catch finally statement marks the block of statements to be tried and specifies a response to throw when an exception occurs.
An error object can be created using the Error constructor. When a runtime error occurs, an instance of the Error will be thrown. The Error object can also be used as the base object for user-defined exceptions. JavaScript has several standard error types built in:
EvalError: Create an error instance that represents the reason for the error: related to eval().RangeError: Create an error instance that represents the reason for the error: a numeric variable or parameter is outside its valid range.ReferenceError: Create an error instance that represents the reason for the error: an invalid reference.SyntaxError: Create an error instance that represents the reason for the error: a syntax error that occurs during the parsing of code by eval().TypeError: Create an error instance that represents the reason for the error: a variable or parameter is not of a valid type.URIError: Create an error instance that represents the reason for the error: the argument passed to encodeURI() or decodeURl() is not valid.window.onerror can capture syntax errors as well as runtime errors. It can obtain error information, stack, error file, line number, and column number. It captures errors from JavaScript scripts executed in the current window, and frontend error monitoring can be implemented using window.onerror. For security reasons, when a syntax error occurs while loading scripts from a different domain, the details of the syntax error will not be reported.