<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">可以是</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">try{}+catch{}</span>
try{}+catch{}+finally{}
try{}+finally{}
三种形式
只要有finally ,肯定就会执行finally语句
下面是几个例子:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"></meta>
<title></title>
</head>
<body>
<script>
try{
try{throw new Error("oops");}
finally{console.log("finally");}
}
catch(ex){console.error("outer",ex.message);}
</script>
</body>
</html>
如果嵌套在里面的try{}catch{}finally{}语句没有catch处理异常,则会在外面处理,但在外面捕获之前会先执行里面的finally语句。如果里面有了catch捕获异常,则不会再在外面处理。
本文探讨了try-catch-finally结构的三种常见形式及其执行逻辑,并通过实例展示了这些结构如何处理异常,特别是在嵌套try-catch-finally语句中的表现。
136

被折叠的 条评论
为什么被折叠?



