Jscript 5.0中的新特性
Jscript 5.0唯一的改变是引入了错误处理。
Java风格的try和catch结构在Jscript 5.0中得到了支持。例如:
Function GetSomeKindOfIndexThingy() {
Try {
// If an exception occurs during the execution of this
// block of code, processing of this entire block will
// be aborted and will resume with the first statement in its
// associated catch block.
Var objSomething = Server.CreateObject (“SomeComponent”);
Var intIndex = objSomething.getSomeIndex();
Return intIndex;
}
catch (exception) {
// This code will execute when *any* exception occurs during the
// execution of this function
alert (‘Oh dear, the object didn’t expect you to do that’);
}
}
内建的Jscript Error对象有3个属性,它们定义了上次的运行期错误。可在catch块中使用它们获得有关错误的更多信息。
Alert (Error.number);
// Gives the numeric value of the error number
// And the result with 0xFFFF to get a ‘normal’ error number in ASP
Alert (error.description);
// Gives an error desciption as a string
假如你想招抛出自己的错误,可用一个定制的异常对象引发一个错误(或异常)。然而,由于没有内建的异常对象,必须自己定义一个结构:
// Define our own Exception object
function MyException (intNumber, strDescripton, strInfo) {
this.Number = intNumber;
// Set the Number property
this.Description = strDescription;
// Set the Description property
this.CustomInfo = strInfo;
// Set some ‘information’ property
}
这样的对象可用来在页面中引发定制的异常。这通过使用throw关键字,然后检查catch块中的异常类型来实现:
function GetSomeKindOfIndexThingy() {
try {
Var objSomething = Server.CreateObject (“SomeComponent”);
Var intIndex = objSomething.getSomeIndex();
If (intIndex == 0) {
// Create a new MyException