Like Java language, JavaScript can throw exceptions through throw statements. Unlike Java language, JavaScript can throw all types of values through throw statements, not only to throw error objects.
The code copy 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 Java language, if the exception is not caught by any catch statement, the exception will eventually be thrown to the user:
The code copy 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 catch throwing exceptions, 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, a parameter e (or any other legal variable name) can be defined to store the thrown outliers. Inside the catch statement, this parameter can be used as a local variable. Unlike other variable usage in JavaScript, parameter variables in catch statements are only valid inside the catch statement (the scope of this 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 the 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 is exited 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 processed by the catch statement, the code in finally is executed.
4. An exception occurs in try, but when the exception needs to be continued to be thrown up due to the absence of a catch statement, the code in finally will be executed. It is worth noting that in the case where the catch statement is missing, JavaScript will first execute the code in finally, and then continue to throw an exception upwards.
In finally code, if break, continue or return statements appear, JavaScript will directly execute these statements, and ignore the break, continue or return statements that may exist in the try code; even if the catch statement is missing, resulting in an exception that needs to be reported, JS will discard the exception reporting information and continue to execute the break, continue or return statements in finally code. Similarly, if an exception is thrown in the finally code, JavaScript will discard all break, continue or return statements in the try code, and also discard possible exception reporting behavior, and only throw exceptions in the finally code.