Explain the exception handling method in JavaScript _ basics

Source: Internet
Author: User
Tags exception handling finally block

There are three kinds 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 explanations.

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

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

When a grammatical 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 code that contains the error not to be executed.
Run-time error:

Execution (after compilation/interpretation) of a run-time error, also known as an exception, 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 method that does not exist:

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

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

A logical error can be the most difficult type of error tracking. These errors are not a result of a syntax or run-time error. Conversely, when an incorrect 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 capability added in the latest version of JavaScript. JavaScript implements a try ... catch ... finally structure and throw operations to handle exceptions.

You can capture programmer generation and Run-time exceptions, but you cannot capture JavaScript syntax errors.

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

<script type= "Text/javascript" >
<!--
try {/
  /Code to run
  [break;]
} catch (e) {
  /Co De to run if a exception occurs
  [break;]
} [Finally {
  //Code is always executed regardless of 
  //a exception occurring
}]
//-->
&L T;/script>

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

Here is an example where we are trying to invoke a function that does not exist, which throws an exception. Let's take a look at its behavior and not have a try ... catch:

 
 

Now, let's try to catch this exception with a try ... catch 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 the statement unconditionally after it is Try/catch forever. Here is an example:

 
 

Throw statement:

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

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

 
 

You can use a string, Integer, Boolean, or object to throw an exception in a function, then you can catch exceptions in the same function that we did on the above, or use try ... catch blocks in other functions.
onerror () syntax

The OnError event handler is the first feature that facilitates JavaScript processing errors. The error event is triggered by the Window object whenever an exception page appears. For example:

 
 

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

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

Here is an example to illustrate how to extract this information

 
 

Can be shown in any way you feel it is better to extract information.

You can use the OnError method to display error messages without any problems loading the image as follows:



You can use the OnError many HTML markup errors to display the appropriate information.

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.