• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

Generating Sensible Error Messages Using Err.Raise

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 30次 | 进入软件测试论坛讨论

领测软件测试网 Okay, I‘’ll admit it, if there‘’s one area where my ASP scripts are lacking: it‘’s in the area of error checking. I‘’ve looked at the Err object included with VBScript but have been really frustrated with it‘’s seemingly lack of information. (For more information on the Err object be sure to read: Error Handling in ASP!) Consider this snippet of code:

<%
  Option Explicit

  Dim Conn
  Dim strSQL

  Set Conn = Server.CreateObject("ADODB.Connection")
  ‘’this DSN does not exist
  Conn.Open "foo"
  ‘’...




If you run the above script (without having a DSN named foo created) you‘’ll get the following error:


Microsoft OLE DB Provider for ODBC Drivers error ‘’80004005‘’
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

/dmsms/etest.asp, line 6

Fine, I can deal with that. While the error message is anything but pretty or profoundly descriptive, I do know that I need to fix something that‘’s wrong on line 6 of the script. So I‘’ll load it into the editor, fix it and then try running it again. If needed I‘’ll repeat this cycle until I have a script that works.

Now consider a script like this one that has Error checking turned on:

<%
  Option Explicit

  On Error Resume Next

  Dim Conn
  Dim strSQL

  Set Conn = Server.CreateObject("ADODB.Connection")
  ‘’this DSN does not exist
  Conn.Open "foo"

‘’... more code ...

  If Err.Number <> 0 then
    Response.Write("Error Number -> " & Err.Number)
    Response.write("<BR>Error Source -> " & Err.Source)
    Response.Write("<BR>Error Desc   -> " & Err.Description)
    Err.Clear
  End If
%>




Viewing the above script through your browser will produce the following output:


Error Number -> -2147467259
Error Source -> Microsoft OLE DB Provider for ODBC Drivers
Error Desc -> [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
To me this information is less useful than the default error message. At least with the default error message I know the line on which the error originated. Recently I was having yet another look at the Err object and I stumbled upon something that I‘’ve overlooked many times in the past. You can raise your own errors!

The Err object contains a Raise method that does exactly this. The Raise method has the following syntax:

object.Raise(number, source, description, helpfile, helpcontext)  


The technical docs for the Raise method can be seen here. A quick note: when raising a custom error, the error number should be a custom error number plus the constant vbObjectError, to make sure that the error number you choose doesn‘’t equal an already predefined error number (we‘’ll see an example of this in the code below).

An example of using the Raise object to generate our own custom error (with a more descriptive message and the line number) can be seen below:

1: <%
2:    Option Explicit
3:    On Error Resume Next
4:
5:    Dim Conn
6:    Set Conn = Server.CreateObject("ADODB.Connection")
7:
8:    ‘’this DSN does not exist
9:    Conn.Open "foo"
10:
11:   If Err.Number <> 0 then
12:     Err.Clear
13:     Err.Raise vbObjectError + 7, _
14:               "etest.asp", "Connection Open Method Failed"
15:   End If
16:   If err.Number <> 0 then    
17:     Response.Write("Error On line    -> " & Err.Number - vbObjectError)
18:     Response.write("<BR>Error Source -> " & Err.Source)
19:     Response.Write("<BR>Error Desc   -> " & Err.Description)
20:     Err.Clear
21:   End If
22: %>




Here‘’s the output from this latest version. Notice that it provides the line number the error occurred on, the page the error occurred on, and a readable and descriptive error message. (Do understand that the reason we know the line number the error occurred on it because we hardcoded the error number (the first parameter) in the Raise method as vbObjectError plus the line number where we think the error might have occurred.)


Error On line -> 7
Error Source -> etest.asp
Error Desc -> Connection Open Method Failed

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网