您的位置:首页 > Web前端 > JavaScript

使用try...catch...and or....finally (vbscript or jscript) 处理可能的某些或全部错误

2007-11-14 14:41 507 查看
例如:vbscript (vbscript支持Try catch)

<script language="vbscript">
function Test(a,b)
Test = a/b
end function
</script>
<script language="javascript">
try
{
alert(Test(6,0));
}
catch(e)
{
alert(e.message);
}
</script>

 

如果是jscript (or 经常使用这个的javascript)

JScript

try...catch...finally 语句

为 JScript 实现错误处理。

try {
tryStatements}
catch(exception){
catchStatements}
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

官方的说法:

On Error Resume Next

This statement just turns on the error handling. In the case of an exception anywhere in the following lines, execution will continue from the line immediately following the offending line, i.e. the line in which you call the function that causes the exception. The equivalent in JavaScript is the classic try...catch statement:

try {
// Exceptions in this block will be handled by the following catch block.
// Any exception will quit this block and switch control to the catch block
}
catch (exceptionObj) {
// Handle exception from the try block above
// Code in this block will be executed before the line immediately
// following the culprit line that caused the exception
}

The major difference in catching exceptions between VBScript and JavaScript is that, once you turned on error handling, you cannot turn it back off in VBScript. Error handling in VBScript is turned off only at the end of the function. In JavaScript, on the other hand, you enclose the block of statements you want to turn on the error handling for with the try block. In short, you can control the start of the block in both VBScript and JavaScript. You can control the end of the block only in JavaScript. The end of the block in VBScript is the end of the function.

/*asp学习网注释:Vbscript和JAVASRIPT 在捕捉意外中的主要区别在于如果前者只在函数的末尾返回错误,VBSCRIPT中除非指定On Error Resume Next,否则遇到错误后会停止执行,但是如果指定On Error Resume Next,我们不能在函数的中间使用Try-catch跳出。而JAVASCRIPT中我们可以控制在函数运行的中间返回。*/

/*www.aspxuexi.com*/

Handling exceptions in VBScript is much more complicated than catching them. In general, you need to explicitly check for error after every suspicious statement:

` turn on error handling
On Error Resume Next

` clear the Err object
Err.Clear

` The following statement or procedure call might cause an exception
Kuku()

` Check if an exception was raised
If Err.Number > 0 then
` handle the exception here
End If

You have noticed the Err object. Exception handling in VBScript is based on it. It has six properties which are set by the recent exception: Number, Description, Source, HelpFile, HelpContext, LastDLLError. The JavaScript's Error object shares two of these properties:

JavaScript's Error Object's Properties VBScript's Err Object's Properties Property Description
Error.number Err.Number Run-time error code. Determined by the script engine or returned from an Automation object as an error code
Error.description Err.Description A textual description of the error

Besides its six properties, the Err object in VBScript has two methods, Clear and Raise. The need for Clear arises from the fact that VBScript does not clear the Err properties after handling an exception. In fact, there is no way for VBScript to detect when the handling of the most recent exception started or ended. You can handle an exception and then check for a new exception even though no new exception is raised. In JavaScript, there is no need for the Clear method as the catch block will be entered only after a new exception has occurred.

JavaScript's Error Object's PropertiesVBScript's Err Object's PropertiesProperty Description
Error.number
Err.Number
Run-time error code. Determined by the script engine or returned from an Automation object as an error code
Error.description
Err.Description
A textual description of the error
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息