try...catch
try...catch Statement marks the statement block to try , And specify a response thrown when an exception occurs .
grammar
try {
// Statements that need to be executed .
// try_statements
}
// If in try The statement executed when an exception is thrown in the block .
catch (exception) {
// catch_statements
}
// stay try A statement block executed after a statement block . Whether or not there is an exception thrown or caught, these statements will be executed .
finally {
// finally_statements
}
describe
try A statement consists of one or more statements try block , And at least one catch A piece or a finally One of the blocks , Or both , Here are three forms of try Statement :
- try...catch;
- try...catch...finally;
- try...finally;
catch Clause contains try The statement to be executed when an exception is thrown in the block . That is to say , You want try The content in the statement succeeded , If it doesn't work , You want to control what happens next , At this time, you can catch Statement . If in try Any statement in the block ( Or from try Block ) Throw an exception , Control immediate steering catch Clause . If in try No exception is thrown in the block , Will skip catch Clause .
finally Clause in try Block and catch Execute after block, but in the next try Execute before declaration . Whether or not an exception is thrown or caught, it always executes .
You can nest one or more try sentence . If inside try The statement did not catch Clause , Then it will enter the package try Of the statement catch Clause .
Conditions catch block
You can also use one or more conditions catch Clause to handle specific exceptions . under these circumstances , When an exception is thrown, the appropriate catch clause . In the following code ,try Block code may throw three exceptions :TypeError,RangeError and EvalError. When an exception is thrown , The control will enter its corresponding catch sentence . If the exception is not specific , Then control will shift to unconditional catch Clause .
When using an unconditional catch Clause and one or more conditional statements , Unconditional catch Clause must be placed last . Otherwise, all exceptions before reaching the conditional statement will be intercepted by the non conditional statement .
Do not conform to the ECMAscript Examples of specifications
try {
myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
// statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
// statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
// statements to handle EvalError exceptions
} catch (e) {
// statements to handle any unspecified exceptions
logMyErrors(e); // pass exception object to error handler
}
accord with ECMAscript Examples of specifications
try {
myroutine();
} catch (e) {
// statements to handle this very common expected error
if (e instanceof TypeError) {
// statements to handle this very common expected error
} else {
throw e; // re-throw the error unchanged
}
}
nesting try block
Example 1
try {
try {
// throw Statement throws an error When a mistake happens JavaScript Will stop and throw an error message
throw new Error('oops');
}
finally {
console.log('finally');
}
}
catch (e) {
console.error('outer', e.message);
}
// Output:
// 'finally'
// 'outer' 'oops'
Example 2
try {
try {
throw new Error('oops');
}
catch (e) {
console.error('inner', e.message);
throw e;
}
finally {
console.log('finally');
}
}
catch (e) {
console.error('outer', e.message);
}
// Output:
// 'inner' 'oops'
// 'finally'
// 'outer' 'oops'
// Any given exception will only be closed by the nearest one catch Block capture once . Of course , stay “inner” Any new exceptions thrown by the block ( because catch The code in the block can also throw exceptions ), Will be “outer” The block captures .
![[technology sharing] after 16 years, how to successfully implement the dual active traffic architecture of zhubajie.com](/img/9f/380dc1a2c1277a216b8c1cd41045bc.jpg)






![[data mining time series analysis] restaurant sales forecast](/img/9a/5b93f447e38fcb5aa559d4c0d97098.png)

