This article mainly introduces JavaScript error handling materials. For more information, see
I. error category
1. Syntax error
It is also known as parsing errors. when a traditional programming language is compiled and interpreted in JavaScript, these errors are directly caused by unexpected characters in the code, then you cannot directly compile/interpret the code. for example, a syntax error occurs when the right parenthesis is missing in a line of code. 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
It is also called an exception (exception, after compilation/interpreter ). At this point, the problem is not in the code syntax, but an operation that is attempted, in some cases, illegal. Eg.
Window. openMyFile ();
The browser returns an exception because the openMyFile () method does not exist. Exceptions only affect the threads that occur. other JavaScript threads can continue normal execution.
II. handling errors
1.Onerror event processing function
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. Eg.
The code is as follows:
Onerror example