Javascript errors-throw and try to catch

Source: Internet
Author: User

TheTryStatement lets you test a block of code for errors.

TheCatchStatement lets you handle the error.

TheThrowStatement lets you create M errors.

Errors will happen!

When the JavaScript engine is executing JavaScript code, different errors can occur:

It can be syntax errors, typically coding errors or typos made by the programmer.

It can be misspelled or missing features in the language (maybe due to browser differences ).

It can be errors due to wrong input, from a user, or from an Internet server.

And, of course, it can be your other unforeseeable things.

Javascript throws errors

When an error occurs, when something goes wrong, the JavaScript engine will normally stop, and generate an error message.

The technical term for this is: javascript willThrowAn error.

Javascript try and catch

TheTryStatement allows you to define a block of code to be tested for errors while it is being executed.

TheCatchStatement allows you to define a block of code to be executed, if an error occurs in the try block.

The javascript statementsTryAndCatchCome in pairs.

Syntax
Try{//Run some code here}Catch(ERR ){//Handle errors here}
The throw statement

The throw statement allows you to create a custom error.

The correct technical term is to create orThrow an exception.

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

Syntax Throw Exception

The exception can be a javascript string, a number, a Boolean or an object.

Example

This example examines the value of an input variable. If the value is wrong, an exception (error) is thrown. The error is caught by the catch statement and a custom error message is displayed:

Example
<SCRIPT> Function  Myfunction (){  Try  {  VaR X = Document. getelementbyid ("Demo" ). Value;  If (X = "") Throw "Empty" ;  If (Isnan (x ))Throw "Not a number" ;  If (X> 10) Throw "Too high" ;  If (X <5) Throw "Too low" ;}  Catch  (ERR ){  VaR Y = Document. getelementbyid ("mess" ); Y. innerhtml = "Error:" + err + "." ;}} </SCRIPT> 

 

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.