Syntax errors, also known as parsing errors, occur in the compilation of traditional languages, occur in the interpretation of JavaScript, and runtime errors are also known as exceptions (exception, after the interpreter during compilation)
I. Error Classification
1. syntax error: it is also called a parsing error. It occurs during compilation in traditional languages and occurs in JavaScript during interpretation. These errors are directly caused by unexpected characters in the Code and cannot be directly compiled/interpreted. When a syntax error occurs, the Code cannot be executed. In JavaScript, only the code in the same thread is affected by syntax errors. Code in other threads and other externally referenced files can be executed if it does not depend on the code that contains the error.
2. runtime error: Also known as exception (exception, after the compilation/Interpreter ). At this point, the problem is not in the code syntax, but an operation that is attempted, in some cases, illegal. Exceptions only affect the threads that occur. Other JavaScript threads can continue normal execution.
Ii. Error Handling
JavaScript provides two methods to handle errors: The onerror event processing function in BOM and the try... catch Method in ECMAScript.
1. onerror event processing functions
It is the first mechanism to assist JavaScript in error handling. When an exception occurs on the page, the error event is triggered on the window object. For example:
The Code is as follows:
Onerror Example