try...catch...finally介绍
2014-03-01 21:23
423 查看
JScript | 语言参考 |
try{
tryStatements}
catch(exception){
catchStatements}
finally {
finally {
finallyStatements}参数
tryStatement
必选项。可能发生错误的语句。
exception
可选项。任何变量名。exception 的初始化值是扔出的错误的值。
catchStatement
可选项。处理在相关联的 tryStatement 中发生的错误的语句。
finallyStatements
可选项。在所有其他过程发生之后无条件执行的语句。
说明:
try...catch...finally 语句提供了一种方法来处理可能发生在给定代码块中的某些或全部错误,同时仍保持代码的运行。如果发生了程序员没有处理的错误,JScript 只给用户提供它的普通错误消息,就好象没有错误处理一样。
tryStatements 参数包含可能发生错误的代码,而 catchStatement 则包含处理任何发生了的错误的代码。如果在 tryStatements 中发生了一个错误,则程序控制被传给 catchStatements 来处理。exception 的初始化值是发生在 tryStatements 中的错误的值。如果错误不发生,则不执行 catchStatements。
如果在与发生错误的 tryStatements 相关联的 catchStatements 中不能处理该错误,则使用 throw 语句来传播、或重新扔出这个错误给更高级的错误处理程序。
在执行完 tryStatements 中的语句,并在 catchStatements 的所有错误处理发生之后,可无条件执行 finallyStatements 中的语句。
请注意,即使在 try 或 catch 块中返回一个语句,或在 catch 块重新扔出一个错误,仍然会执行 finallyStatements 编码。 一般将确保 finallyStatments 的运行,除非存在未处理的错误。(例如,在 catch 块中发生运行时错误。。)
示例
下面的例子阐明了JScript 特例处理是如何进行的。
try {
print("Outer try running..");
try {
print("Nested try running...");
throw "an error";
}
catch(e) {
print("Nested catch caught " + e);
throw e + " re-thrown";
}
finally {
print("Nested finally is running...");
}
}
catch(e) {
print("Outer catch caught " + e);
}
finally {
print("Outer finally running");
}
// Windows Script Host
作出该修改从而得出WScript.Echo(s)
function print(s){
document.write(s);
}
将得出以下结果:
Outer try running..
Nested try running...
Nested catch caught an error
Nested finally is running...
Outer catch caught an error re-thrown
Outer finally running
相关文章推荐
- 【随笔】写在闪电孵化器分享会之后
- Linux服务器操作系统基础篇
- tomcatx java.lang.ClassCastException: org.apache.jasper.el.ELContextImpl
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties
- CF 396A On Number of Decompositions into Multipliers
- win7系统右键资源管理器崩溃的解决办法
- 经验分享一 自定义ArrayAdapter中的字体
- ACM编程技巧--常用字符操作函数
- 关于笔记本linux亮度调节
- 【uni环境高级编程】2.进程控制笔记
- android Canvas的save和restore(截图)
- Gas Station
- HDU 4004
- myeclipse添加server library
- MutableCopy 与 Copy
- MyEclipse项目里面出现红叉,项目内又没有红叉的解决方案
- 九度oj 题目1015:还是A+B 【ZJU2006考研机试题1】
- The steps for download android source code
- Linux开发网站
- FusionWidgets之AngularGauge图