"Turn" to explain the exception handling method in JavaScript

Source: Internet
Author: User
Tags finally block

There are three types of programming errors: (1) syntax errors and (2) run-time error (3) logic errors:
Syntax error:

Syntax errors, also known as parsing errors, are performed at compile time in a traditional programming language and appear in JavaScript interpretation.

For example, the following line will result in a syntax error because it lacks a closing parenthesis:

<script type= "Text/javascript" ><!--window.print (;//--></script>

When a syntax error occurs in JavaScript, only the syntax errors contained in the same thread are affected, the code in the other thread is executed, and the code relies on the code that contains the error is not executed.
Run-time error:

Execution (after compilation/interpretation) at run-time errors, also known as exceptions, is raised.

For example, the following line will cause a run-time error because the syntax here is correct, but at run time it is trying to invoke a non-existent method:

<script type= "Text/javascript" ><!--window.printme ();//--></script>

Exceptions also affect the threads in which they occur, allowing other JavaScript threads to continue to execute normally.
Logic error:

Logic errors can be the most difficult type of error tracing. These errors are not a result of a syntax or run-time error. Conversely, when an erroneous driver script logic occurs, you do not get the desired result.

You may not be able to catch these errors because it depends on what type of logic the program is based on business requirements.
Try...catch...finally statement:

The exception handling capabilities added in the latest version of JavaScript. JavaScript implements the Try ... catch ... finally struct and throws an action to handle the exception.

You can capture programmer-generated and run-time exceptions, but you can't catch JavaScript syntax errors.

Here is the try...catch...finally block syntax:

<script type= "Text/javascript" ><!--try {  //code to run  [break;]} catch (e) {  //code to run if an Exception occurs  [break;]} [Finally {  //Code that was always executed regardless of   //an exception occurring}]//--></script>

The try block must be followed by only one catch block or one finally block (or both). When an exception is placed in the try block, it is executed in addition to the E and catch blocks. The optional finally block after the Try/catch statement executes unconditionally.
Example:

Here is an example of an attempt to invoke a nonexistent function that throws an exception. Let's take a look at its behavior, not having a try ... catch:

Now, let's try to use the try ... catch to catch this exception and display a user-friendly message. You can also cancel this message if you want to hide this error from the user.

You can use the finally block to execute unconditionally after the statement is Try/catch forever. Here is an example:

Throw statement:

You can use the throw statement to improve your built-in or custom exceptions. These anomalies can then be captured and the appropriate action can be taken.

The following is an example of the use of a throw statement.

You can use strings, integers, booleans or objects to throw an exception in a function, then you can catch exceptions in the same function that we did above, or use the Try ... catch block in other functions.
OnError () syntax

The OnError event handler is the first feature that allows JavaScript to handle errors. The error event is triggered whenever a Window object appears on an exception page. For example:

The OnError event handler provides three information to identify the exact nature of the error:

    1. The error message. The browser will display the same message as the given error
    2. Url. In the file where the error occurred
    3. Line number. The line number given in the URL that caused the error

Here is an example to illustrate how to extract this information

Can be displayed in any way that you think it is better to extract information.

You can use the OnError method to display error messages in cases where there are no problems loading images as follows:

You can use the onerror to display the appropriate information in the case of many HTML tags incorrectly.

You may be interested in

    1. Detailed analysis of 1000+ project Data 10 big JavaScript errors
    2. 10 Advanced tips for debugging with the console
    3. Where there's a browser, there's fundebug.
    4. Debug Front End Html/css

"Go" for an explanation of how exceptions are handled in JavaScript

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.