Home > Web Front-end > JS Tutorial > body text

Introduction to exception catching in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:23:12
Original
1173 people have browsed it

Same as the Java language, JavaScript can throw exceptions through the throw statement. Unlike the Java language, JavaScript can throw all types of values ​​through the throw statement, not just error objects.


Copy code The code is as follows:

//Throw an Error object.
try{
throw new Error("Message in Error Object");
}catch(e){
console.log(e);//Error: Message in Error Object
}

try{
throw "Raw Message";
}catch(message){
console.log(message);//Raw Message
console.log(typeof message);//string
}

try{
throw 42;
}catch(code){
console.log(code);//42
console.log(typeof code);//number
}


Like the Java language, if the exception is not caught by any catch statement, the exception will eventually be thrown to the user:


Copy code The code is as follows:

try{
//throw new Error("test error");//Error will be thrown. Error: test error
}finally{
}

try{
throw 42;//Error will be thrown. Error: 42
}finally{
}


For catching exceptions thrown, JavaScript also uses try/catch/finally statements. The usage rules are: try is required, catch and finally are optional statements, but at least one of catch and finally must appear.

In the catch statement, you can define a parameter e (or any other legal variable name) to store the thrown exception value. Inside the catch statement, this parameter can be used as a local variable. Unlike other variable usage in JavaScript, the parameter variable in the catch statement is only valid within the catch statement (the scope of the variable is limited to the catch statement).

For finally statements, the code in finally will be executed regardless of whether there is an exception thrown in try. Details include:

1. No exception occurs in try. When the try statement is executed, the code in finally will be executed.
2. No exception occurs in try, but when the try code exits due to execution of break, continue or return statements, the code in finally will be executed.
3. An exception occurs in try. After the exception is handled by the catch statement, the code in finally is executed.
4. When an exception occurs in try, but the exception needs to continue to be thrown upward because there is no catch statement, the code in finally will be executed. It is worth noting that in the absence of a catch statement, JavaScript will first execute the code in finally and then continue to throw an exception.

In the finally code, if a break, continue or return statement appears, JavaScript will directly execute these statements, regardless of the break, continue or return statements that may exist in the try code; even if the absence of the catch statement causes an exception, When reported, JS will also discard the exception reporting information and continue to execute the break, continue or return statements in the finally code. Similarly, if an exception is also thrown in the finally code, JavaScript will discard all break, continue, or return statements in the try code, and also discard possible exception reporting behaviors, and only throw the exception in the finally code.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template