Microsoft Asp.Net Ajax框架入门(6) Debugging and Typed Errors

VS2008

本文介绍在开发客户端Javascript的时候,如何输出调试信息,以及如何定义自定义异常类型并捕获。

1. Sys.Debug
    Microsoft AJAX Library提供了一个Sys.Debug的类,用于调试的时候将调试信息输出,有了它我们就不再需要使用蹩脚的alert的方法了。
    为了让调试跟踪信息在页面上展现,我们需要在页面上加入一个名为TraceConsole的的textarea控件
< textarea id = " TraceConsole "  cols = " 50 "  rows = " 10 " ></ textarea >

1.1 Sys.Debug.trace
    方法定义 Sys.Debug.trace(text)
    这个方法的功能是:将一个跟踪文本信息在TraceConsole中显示
Sys.Debug.trace( " this is a debug message " );
    页面执行完之后就会在TraceConsole中显示这句文本。
1.2 Sys.Debug.traceDump
    方法定义 Sys.Debug.traceDump(Object, string name)
    这个方法的功能是:将一个对象的所有属性信息(包括值)在TraceConsole中显示
    假设我页面上有一个名为"form1"的元素,
Sys.Debug.traceDump(Sys.UI.DomElement.getLocation( $ get ( " form1 " ) ));
    那么,在TraceConsole中我将看到如下信息:
     traceDump {Sys.UI.Point}
    x: 10
    y: 15

1.3 Sys.Debug.assert
    方法定义 Sys.Debug.assert(Boolean condition, string message, Boolean displayCaller)
    也就是我们在Unit Test中常见的断言,当condition为false的时候,脚本执行会异常终止,并提示message信息,如果displayCaller为true的话,将显示断言所属的调用者(函数)
    
Sys.Debug.assert( 1 > 2 " 1 怎么会大于 2呢 " false );
    这时会有一个windows警告框弹出,并提示 message 信息。

2. 异常与try...catch代码块
2.1 使用内建的异常类型

    在Error下的几个方法可以帮助我们创建几个常用的内建的异常类型
    例:
try   {
                
throw Error.argumentNull();
            }

            
catch (e)  {
                Sys.Debug.traceDump(e);
            }

    TraceConsole中输出的信息为:
     traceDump {Error}
    description: Sys.ArgumentNullException: Value cannot be null.
    message: Sys.ArgumentNullException: Value cannot be null.
    name: Sys.ArgumentNullException
    paramName: Undefined

2.2 使用自定义异常类型
    可以通过Error的create自定义异常类型:
var myError  =  Error.create( " myError " {name: " xxx", desc : "my cc"} );
            
try   {
                
throw myError;
            }

            
catch (e)  {
                Sys.Debug.traceDump(e);
            }
    
    TraceConsole输出:

traceDump {Error}
    description: myError
    message: myError
    name:  xxx
    desc: my cc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值