July 31 = 311-315

Source: Internet
Author: User
Tags finally block

13.6.1 statement Block

The so-called statement block refers to multiple statements contained in curly brackets. The statement block is an overall execution body, similar to a separate statement.

Code example:

{

A = 1;

B = 2;

Alert (A + B );

}

 

13.6.2 null statement

The simplest null statement has only one semicolon (;)

Code example:

// Empty statement

;

// Use an empty statement to initialize the array.

VaR A = [];

For (VAR I = 0; I <10; A [I ++] = I + 20 );

 

13.6.3 an exception is thrown.

Javascript supports Exception Handling and manual throws. When JavaScript needs to throw an exception, it always throws an error object through the throw statement.

The syntax for throwing an error object is as follows:

Throw new error (errorstring );

// Manually throw a custom exception

For (VAR I = 0; I <10; I ++)

{

If (I <4)

Throw new error ('custom exception error ');

}

 

13.6.4 exception capture statement

When a program encounters an exception, you can use catch to catch the exception, whether it is a manually thrown exception or an exception of the system itself. JavaScript Code Execution

If an exception occurs, the program jumps to the corresponding catch block.

Code example:

Try

{

For (VAR I = 0; I <10; I ++)

{

If (I> 4)

{

Throw new error ('user-defined exception ');

}

}

}

// If the code in the try block encounters an exception, the system automatically jumps to the Catch Block for execution.

Catch (E)

{

Document. writeln ('System exception '+ E. Message +' <br> ');

}

// The Finally block code can always be executed.

Finally

{

Document. writeln ('System finally Block ');

}

Javascript has only one exception class: error.

Javascript is a weak type language, so the exception instance in the catch statement parentheses does not need to declare the type.

Except ript has only one exception class, and only one catch block can be created after all try blocks.

The exception description is obtained through the message attribute of the exception object.

 

13.6.5 with statement

The with statement is a more concise method. Using the with statement can avoid repeated object writing.

Code example:

// The with statement is not used.

Document. writeln ("1 ");

Document. writeln ("2 ");

Document. writeln ("3 ");

// The with statement is used.

With (document)

{

Writeln ("1 ");

Writeln ("2 ");

Writeln ("3 ");

}

  

The with statement is mainly used to avoid repeated Writing of the same object.

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.