跟我从头学WSH(4)–错误处理和Debug

作者:qiqinghua
来源:青蛙的梦想

一。错误处理

(1)错误类型
语法错误
比如错误的关键词,错误的构造(缺少next的for)
运行时错误
语法正确,但语句尝试执行一个不可能的操作,比如除以0,或向软盘存储,但软盘不存在
逻辑错误
执行没有达到预期的结果。

(2) 处理运行时错误
创建robust script
通过预设一些处理脚本,用来处理用户错误,使得不要中断程序运行。
使能Graceful Exits
有些时候,error-handling code不能解决一个run-time错误,这个时候,你的代码仍旧执行一个动作,比如关闭所有打开的数据文件并保存数据,同时,通知用户一个错误发生,并进行更多的描述。
实现错误处理
设置一个错误陷阱
确定发生什么错误
执行一些补救的动作或用gracefully script退出。

(3)错误对象
Err object属性
常见的属性包括:
Number: 返回一个合法的错误数字,number是err object的默认属性
Description: 错误消息
Err object 方法:
Clear 清楚所有的err object的属性设置,clear等价于err=0
Raise 生成一个run-time错误,你可以使用这个方法来测试错误处理代码。

(4) 设置一个错误陷阱
On Error Resume Next
当陷阱被设置之后,默认的vbscript处理运行时错误的动作将被替代,这使得你可以用你自己的代码来控制动作的顺序,(使错误处理更合适,更安全)。
通过设置On Error Resume Next语句设置错误陷阱,这将使得你的代码继续运行。
如果你采用了on error resume语句,你应该对可能出现的潜在错误在代码中都有所反映:
On Error Resume Next
Dim intNumberOfTests, intTotalPoints, nAverage
' collect test information
intTotalPoints = InputBox("Enter the Total points scored.")
intNumberOfTests = InputBox("Enter the number of exams.")
' calculate average
nAverage = intTotalPoints / intNumberOfTests
' check for runtime errors by zero
Select Case Err.Number
Case 0 'No error occurred
WScript.Echo "Average score = " & Str(lngAverage)
Case 11 'Division by zero
WScript.Echo "Cannot divide by zero."
Case 13 ' Type mis-match
WScript.Echo "Only numeric data is allowed."
Case Else
WScript.Echo "Unexpected error."
WScript.Quit
End Select
Fixing the Error
上面的例子并没有修复错误,只是顺利的退出,下面的例子是尝试修复错误:也就是让用户再次尝试。
Call MainProc
Sub MainProc()
Dim iResult, iAnswer
iResult = CalcAvg()
Select Case iResult
Case 1
WScript.Echo "Cannot divide by zero"
iAnswer = Msgbox("Do you want to try again?",vbYesNo)
If iAnswer = vbYes then
Call MainProc
Else
WScript.Quit
End IF
Case 2
WScript.Echo "Only numeric data is allowed"
iAnswer = Msgbox("Do you want to try again?",vbYesNo)
If iAnswer = vbYes then
Call MainProc
Else
WScript.Quit
End IF
Case 3
WScript.Echo "An unexpected error occurred"
iAnswer = Msgbox("Do you want to try again?",vbYesNo)
If iAnswer = vbYes then
Call MainProc
Else
WScript.Quit
End IF
End Select
End Sub
Function CalcAvg()
On Error Resume Next
Dim intNumberOfTests, intTotalPoints, nAverage
' collect test information
intTotalPoints = InputBox("Enter the Total points scored.")
intNumberOfTests = InputBox("Enter the number of exams.")
' calculate average
nAverage = CInt(intTotalPoints)/CInt(intNumberOfTests)
' check for runtime errors by zero
Select Case Err.Number
Case 0 'No error occurred
WScript.Echo "Average score = " & nAverage
CalcAverage = 0
Case 11 'Division by zero
CalcAvg = 1
Case 13 ' Type mis-match
CalcAvg = 2
Case Else
CalcAvg = 3
End Select
End Function

二。排错
(1) Microsoft script debugger
Microsoft script debugger是作为windows 2000的安转的一部分,它帮助你找到逻辑错误。
查看源代码
查看和控制脚本流
查看和改变变量和属性的值
查看脚本中过程执行的顺序
在WSH脚本中处理错误
过程:
?脚本被调入WSH进行解析
?报告在解析期间的任何语法错误
?在成功解析后,WSH执行脚本
?当运行时错误发生,将显示错误信息并停止执行。
使用microsoft script debugger
当错误发生的时候,WSH会指示发生错误的行,如果你在计算机上安装有debuger,你可以尝试排错脚本。以只读方式打开当前的脚本。
你不可以直接在debugger中编辑脚本,你必须修改原来的.vbs文件。

(2) 排错技术
设置断点和步进
To set a breakpoint on a specific line, take the following actions:
In the Microsoft Script Debugger, place the insertion point in the line where you want the breakpoint.
On the Debug menu, click Toggle Breakpoint.
The line where you set the breakpoint is displayed in red to indicate that it is a breakpoint.
Step Into:每步执行一行
Step Over:同样每步执行一行,但子过程中不分步执行。
Step Out: 在长的脚本中,指定某一段落全速执行
使用命令窗口
查看和改变值,可以使用命令窗口。
查看call stack
用来跟踪过程的调用
You can open the Call Stack window from the View menu. You can then use the list of procedure calls to trace the execution

相关日志

发表评论