JavaScript Errors and debugging

Source: Internet
Author: User

1.JavaScript errors-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.

1.1 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.

1.2JavaScript throw (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.

1.3JavaScript 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.

Try {  // here to run code catch(err) {  // handle error here }
vartxt=""; function message () {Try{Adddlert ("Welcome guest!"); } Catch(err) {txt="there is an error on this page. \ n"; TXT+="Error Description:"+ Err.message +"\ n"; TXT+="Click OK to continue. \ n";     alert (TXT); } }

1.3Throw statements

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.

Throw exception

An exception can be a JavaScript string, a number, a logical value, or an object.

This example detects the value of the input variable. If the value is wrong, an exception (error) is thrown. Catch catches this error and displays a custom error message:

function MyFunction () {varmessage, X; Message= document.getElementById ("message"); Message.innerhtml=""; X= document.getElementById ("Demo"). Value; Try {         if(x = ="")Throw "value is empty"; if(IsNaN (x))Throw "not a number ."; X=Number (x); if(X <5)Throw "too small"; if(X >Ten)Throw "too Big"; }    Catch(Err) {message.innerhtml="Error:"+err; }}

2.JavaScript Commissioning

2.1 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:

56= a + B;console.log (c);

2.2 Setting 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).

2.3debugger 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.

JavaScript errors and debugging

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.