Describes how to handle exceptions in JavaScript and how to handle exceptions in javascript.

Source: Internet
Author: User
Tags finally block

Describes how to handle exceptions in JavaScript and how to handle exceptions in javascript.

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

Syntax errors, also known as parsing errors, are implemented in traditional programming languages during compilation and occur during JavaScript interpretation.

For example, the following line may cause a syntax error because it lacks an angle bracket:

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

When a syntax error occurs in JavaScript, only the impact of the syntax error contained in the same thread is executed in other threads; code dependent on code that contains errors will not be executed.
Runtime error:

Execution (after compilation/interpretation) is also known as an exception during runtime, which may cause errors.

For example, the following line will cause a runtime error because the syntax here is correct, but it is trying to call non-existing methods at runtime:

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

Exceptions also affect the threads they occur and allow other JavaScript threads to continue normal execution.
Logical error:

Logical errors may be the most difficult type of Error Tracking. Are these errors a result of a syntax or runtime error. On the contrary, when an error occurs in the drive script logic, you do not get the expected results.

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

Exception Handling capability added in the latest version of JavaScript. JavaScript implements the try... catch... finally structure and throws an operation to handle exceptions.

You can capture exceptions generated and run by programmers, but cannot 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 is always executed regardless of   // an exception occurring}]//--></script>

The try block must be followed by only one catch block or one finally block (or one of the two ). When an exception occurs in the try block, except for the e and catch blocks, the exception is executed. Optional finally blocks after a try/catch statement are executed unconditionally.
Example:

The following is an example. We are trying to call a non-existent function, which will cause an exception. Let's take a look at its behavior. It does not have try... catch:

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

You can use the finally block to execute the try/catch statement permanently. The following is an example:

Throw statement:

You can use throw statements to improve your built-in exceptions or custom exceptions. Later, these exceptions can be captured and appropriate actions can be taken.

The following is an example of throw statement usage.

You can use a string, integer, Boolean, or object to throw an exception in a function, so you can capture exceptions in the same function. We did this, or use try... catch Block in other functions.
Onerror () syntax

The onerror event handler is the first feature that facilitates JavaScript to handle errors. An error event is a window object triggered every time on an exception page. For example:

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

  1. Error message. the browser will display the same message for the given error
  2. URL. In the file where an error occurs
  3. Row number. The row number given in the URL that causes the error

Here is an example to illustrate how to extract this information

It can be displayed in any way and you think it is better to extract information.

You can use the onError method to display the error message without loading the image as follows:


You can use onerror to display the corresponding information when many HTML tags are incorrect.

Related Article

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.