新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 软件开发 > Web开发 > Asp教程 > 正文:Jscript 5.0中的新特性

Jscript 5.0中的新特性

新客网 XKER.COM 2003-07-11 来源: 收藏本文
 
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 object
                     theException = new MyException (0x6F1, “Zero index not permitted”,
                                                                             “Index_Err”);
                     throw theException;
              }

              catch (objException) {
                     if (objException instanceof MyException) {
                            // This is one of our custom exption objects
                            if (objException.Category == “Index_Err”) {
                                   alert (‘Index Error: ‘ + objException.Description);
                            else
                                   alert (‘Undefined custom error: ‘ + objException.Description);
                            }
                     else
                            // Not “our” exception, so display it and raise to next higher routine
                            alert (Error.Description + ‘ (‘ + Error.Number + ‘)’);
                            throw exception;
                     }
              }
       }
收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐