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.