javascript 的 try 和 catch 语句

try{
    
    
	throw "test";
} catch (ex) {
    
    
	console.log(ex); //test
} finally {
    
    
	console.log('finally')
}

它的执行流程:
首先执行 try 块中的代码, 
如果抛出异常由 catch 捕获并执行, 如果正常的话, catch 中的代码被忽略.
不管有没有异常最后都执行 finally 的代码.

try 后面必须接一个 catch 或者 finally
因此 js 中的 try catch 语句有三种形式
1. try catch
2. try finally
3. try catch finally

猜你喜欢

转载自blog.csdn.net/m0_48446542/article/details/108969372
今日推荐