Java Script Learning 3

Source: Internet
Author: User

Javascripterror-Throw, try, and catch

The try statement tests the code block for errors.

The catch statement handles the error.

The throw statement creates a custom error.

JavaScript Error

Various errors occur when the JavaScript engine executes JavaScript code.

It could be a syntax error, usually a coding error or typo caused by a programmer.

It may be a misspelling or a missing feature in the language (possibly due to browser differences).

The error may be caused by error output from the server or user.

Of course, it may also be due to many other unpredictable factors.

JavaScript throws (throw) error

When an error occurs, the JavaScript engine usually stops and generates an error message when something goes wrong.

The technical term that describes this situation is that JavaScript throws an error.

JavaScript try and catch

The try statement allows us to define a block of code that performs error testing at execution time.

The catch statement allows us to define a block of code that executes when a try block of code has an error.

The JavaScript statement try and catch are paired occurrences.

Throw statement

The throw statement allows us to create custom errors.

The correct technical term is: Create or throw an exception (exception).

If you use throw with try and catch, you can control the flow and generate custom error messages.

When writing JavaScript, it would be painful to have no debugging tools.

JavaScript debugging

It is difficult to write JavaScript programs without Debugging tools.

Your code may contain syntax errors, logic errors, and if there are no debugging tools, these errors are more difficult to find.

In general, if there is an error in JavaScript, there is no prompt, so you cannot find the location of the code error.

JavaScript Debugging Tools

Looking for errors in program code is called Code debugging.

Debugging is difficult, but fortunately, many browsers have built-in debugging tools.

The built-in debugging tools can be started or closed, and critical error messages are sent to the user.

With the debugging tools, we can set breakpoints (where the code stops executing), and you can detect variables as the code executes.

The browser-enabled debugging tool typically presses the F12 key and selects "Console" in the Debug menu.

Console.log () method

If your browser supports debugging, you can use the Console.log () method to print JavaScript values on the debug window:

Set breakpoints

In the Debug window, you can set breakpoints for JavaScript code.

On each breakpoint, the execution of the JavaScript code is stopped so that we can check the value of the JavaScript variable.

After the check is complete, you can re-execute the code (such as the play button).

Debugger keywords

The debugger keyword is used to stop executing JavaScript and invoke the Debug function.

This keyword has the same effect as setting breakpoints in the Debug tool.

If no debugging is available, the debugger statement will not work.

Turn on debugger, and the code stops executing before the third line.

Debugging Tools for major browsers

Typically, a browser-enabled debugging tool typically presses the F12 key and selects "Console" in the Debug menu.

The steps for each browser are as follows:

Chrome
    • Open your browser.
    • In the menu, select Tools.
    • Select the Developer tool in the tool.
    • Finally, select Console.
Firefox browser
    • Open your browser.
    • Visit page:
      Http://www.getfirebug.com.
    • Follow the instructions:
      Install Firebug.
Internet Explorer browser.
    • Open your browser.
    • In the menu, select Tools.
    • Select the Developer tool in the tool.
    • Finally, select Console.
Opera
    • Open your browser.
    • Opera's built-in debugging tools are Dragonfly, with detailed descriptions of accessible pages:
      http://www.opera.com/dragonfly/.
Safari
      • Open your browser.
      • Right-click the mouse and select check elements.
      • Select "Console" in the window that pops up at the bottom.
        • JavaScript variable Promotion

          In JavaScript, the declarations of functions and variables are promoted to the topmost part of the function.

          In JavaScript, variables can be declared after they are used, that is, variables can be declared using the first.

        • JavaScript initialization does not elevate

          JavaScript only declares a variable that is promoted and is not initialized

        • Declare your variables in the head

          For most programmers, JavaScript variable elevation is not known.

          If the programmer does not understand the variable promotion well, the program they write is prone to some problems.

          To avoid these problems, we usually declare these variables before each scope starts, which is also a normal JavaScript parsing step, which is easy for us to understand.

Java Script Learning 3 (RPM)

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.