Home  >  Article  >  Web Front-end  >  JavaScript error handling_javascript skills

JavaScript error handling_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:16:021567browse

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.

Copy code The code is as follows:

                                                                                                                   
                                                                                                                                                                                          onerror example
                                  
        
         
        


In the above code, if you try to call a non-existent function when the page is loading, an exception will be thrown. An "An error occurred" error message pops up. However, the browser's error message is also displayed. How to hide it on the browser? Just return a true from the onerror method.

Copy code The code is as follows:


1) Remove error message

The onerror handler provides three types of information to determine the exact nature of the error:

i) Error message - for a given error, the browser will display the same message;

ii) URL - in which file the error occurred;

Line number – The line number in the given URL where the error occurred.

See the following example for the access method:

Copy code The code is as follows:


2) Image loading error

The window object is not the only object that supports onerror event handling functions, it also provides support for image objects. When an image fails to load successfully due to reasons such as file non-existence, the error event is triggered on the image. Let’s look at an example:

The above example directly assigns the onerror event handler function in HTML. Of course, event processing functions can also be assigned through scripts. Before setting the src characteristics of the image, you must wait for the page to be fully loaded. The code is as follows:

Copy code The code is as follows: