Vs2008
This article describes how to output debugging information when developing the client JavaScript, and how to define and capture custom exception types.
1. SYS. debug
the Microsoft Ajax Library provides a sys. the debug class is used to output debugging information during debugging. With this class, we no longer need to use the lame alert method.
to display debugging tracing information on the page, you must add a textarea control named traceconsole on the page. textarea id = " traceconsole " Cols = " 50 " rows = " 10 " > textarea >
1.1 sys. debug. trace
method definition sys. debug. trace (text)
the function of this method is to display sys. debug. trace ( " This is a debug message " );
after the page is executed, the text is displayed in the traceconsole.
1.2 sys. debug. tracedump
method definition sys. debug. tracedump (object, string name)
the function of this method is to set all attributes (including values) of an object) show in traceconsole
assume that there is an element named "form1" on my page, sys. debug. tracedump (sys. UI. domelement. getlocation ($ Get ( " form1 " )));
In the traceconsole, I will see the following information:
Tracedump {SYS. UI. Point}
X: 10
Y: 15
1.3 SYS. Debug. Assert
Method definition SYS. Debug. Assert (Boolean condition, string message, Boolean displaycaller)
That is, our common assertions in unit test. When the condition is false, the script execution ends abnormally and the message is displayed. If displaycaller is true, the caller (function) to which the assertion belongs will be displayed)
SYS. Debug. Assert ( 1 > 2 , " How can 1 be greater than 2? " , False );
A Windows warning box pops up with a message.
2. Exceptions and try... catchCodeBlock
2.1 use the built-in exception type
Several methods under error can help us create several common built-in exception types
Example: Try {
ThrowError. argumentnull ();
}
Catch (E) {
SYS. Debug. tracedump (E );
}
The output information in the traceconsole is:
Tracedump {error}
Description: SYS. argumentnullexception: value cannot be null.
Message: SYS. argumentnullexception: value cannot be null.
Name: SYS. argumentnullexception
Paramname: Undefined
2.2 Use a custom exception type
You can use create in error to customize the exception type: VaR myerror = Error. Create ( " Myerror " , {Name:"Xxx", Desc:"My CC"} );
Try {
ThrowMyerror;
}
Catch (E) {
SYS. Debug. tracedump (E );
}
Traceconsole output:
Tracedump {error}
Description: myerror
Message: myerror
Name: XXX
Desc: My CC