1. Error classification
1. Syntax error
Also known as parsing errors, which occur at compile time in traditional programming languages, and at interpret time in JavaScript, these errors are directly caused by unexpected characters in the code, which then cannot be directly compiled/interpreted, eg, in A line of code produced a syntax error due to a missing right parenthesis. When a syntax error occurs, code execution cannot continue. In JavaScript, only code within the same thread is affected by syntax errors. Code in other threads and in other externally referenced files can continue to execute if it does not depend on the code containing the error.
2. Runtime error
Also known as exception (exception, at compile time/after interpreter). At this point, the problem is not with the syntax of the code, but with trying to complete an operation that is illegal in some cases. eg.
window.openMyFile();
Because the openMyFile() method does not exist, the browser will return an exception. The exception only affects the thread where it occurred, and other JavaScript threads can continue to execute normally.
2. Handling errors
1. onerror event handling function
It is the first mechanism used to assist JavaScript in handling errors. When an exception occurs on the page, the error event is triggered on the window object. Eg.