A Brief introduction to JavaScript exception handling statements

  • 2020-06-22 23:50:05
  • OfStack

It is inevitable that there will be errors during the running process of the program, and the running result after the error is often incorrect, so the program with the error during the running time is usually forced to stop. Runtime errors are collectively called exceptions, and JavaScript provides exception handling statements in order to get a chance to handle an error when it occurs. Contains ES2en-ES3en, ES4en-ES5en-finally and throw.

try - catch statements


try{
tryStatements
}
catch(exception){
catchStatements
}

Parameter description:
tryStatements: Required. A sequence of statements that can cause errors.
exception: Required. Any variable name used to refer to the error object when the error occurred.
catchStatements: Optional. Error handling statement to handle errors that occur in tryStatements.
When coding, it is common to write potentially incorrect statements into the curly braces of the try block and handle errors in the catch block that follows. The error message is contained in an error object (Error object) that can be accessed through a reference to exception. Based on the error information in the error object to determine how to handle it.


<script type="text/javascript">
try{
var n = error; // man-made 1 A mistake, error Used undefined 
}
catch(e){
alert((e.number&0xFFFF) + " Error: " + e.description); // Error handling: Output error messages only 
}
</script>

This code segment USES an try-ES33en structure handler runtime error, and the fourth pedestrian causes an error. The catch block in lines 6-9 catches the error and handles it.
Tip: JavaScript's errors are runtime errors and syntax errors, which are found at compile time; Runtime errors are discovered during the run, and error-handling statements only handle runtime errors.

try catch - finally statements


try{
tryStatements;
}
catch( exception ){
handleStatements;
}
finally{
fianllyStatements;
}

Parameter description:
tryStatements: required, statement that may throw an exception.
handleStatements: Optional, exception handling statement.
fianllyStatements: Optional statement that is unconditionally executed after the completion of other procedures.
The statement in the finally block is executed at the end, although no error occurs, and the program code for resource cleaning is usually placed here.
An artificial exception is thrown when traversing an array with the apple name.


<script type="text/javascript">
try{
var fruit = new Array(" pears "," apple "," grapes "," plum ");

for( n=0;n<fruit.length;m++)
{
document.write(fruit[n] + "");
}
}
catch( e )
{
alert( (e.number&0xFFFF) + " Error: " + e.description );
}
finally{
fruit = null;
alert("fruit="+fruit+" Has been disconnected fruit Array reference! ");
}
</script>

Line 5 of this code snippet artificially throws an exception with an undefined variable m. Lines 11 through 13 catch and handle exceptions. The finally block in lines 14-18 cleans up the resource. This statement is executed unconditionally to ensure that the resources occupied by the fruit array are not leaked.

throw statement

Multiple exception handling statements can be nested. When multiple structures are nested, the ES68en-ES69en statement in the inner layer can throw an exception without intending to handle it itself. The parent try-catch statement can receive exceptions thrown by the child, and the throw operation USES the throw statement.
throw expression;
The value of the expression is passed out as an error message object that will be captured by the catch statement. The throw statement can be used anywhere you want to throw an exception.
In general, 0 is not a divisor, so you can define and throw an exception for a divisor of 0.


<script>
try{
var dividend = 100; // dividend 
var parts = 0; // divisor 
if( parts == 0){ // If the divisor is PI 0 Throw an exception 
throw "Error:parts is zero"; // An exception is thrown 
}
alert(" each " + dividend/parts + " Copy of the "); // Output prompt message 
}
catch(e){ // Here you will capture try An exception thrown in a block 
alert(e); // Use the dialog box to output information about the error object 
}
</script>

This is the end of this article, I hope you enjoy it.


Related articles: