Generating Sensible Error Messages Using Err.Raise

error

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: 

时间: 2024-09-10 00:55:03

Generating Sensible Error Messages Using Err.Raise的相关文章

用err.raise自定义错误信息

错误 我想大多数的人在编写ASP程序的时候,都碰到过类似的错误信息:   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 可时间上,这个错误信息对我们并不怎么有用,或

User Tips: Using Return Values from a SQL Server Stored Procedure to Customize Error Messages

error|server When I started developing web pages that interact with a SQL Server database, I probably started like everbody else: with inline SQL statements. I then progressed to using the connection object to call stored procedures and eventually st

SLF4J warning or error messages and their meanings(转)

  The method o.a.commons.logging.impl.SLF4FLogFactory#release was invoked. Given the structure of the commons-logging API, in particular as implemented by SLF4J, the o.a.commons.logging.impl.SLF4FLogFactory#release()method should never be called. How

解决IIS下UTF-8文件报错乱码问题

解决IIS下UTF-8文件报错乱码的问题 原因: 你的网站是utf-8编码,但iis的错误信息输出默认使用的是gb2312编码.导致iis出现运行时错误的时候显示的页面是乱码. 解决办法: [办法1] 自己处理错误,不让iis来处理. 在所有可能出错的地方,都用类似下面的方式来捕捉. On Error Resume Next Err.Raise 6 '这行是可能出错的代码 If Err Then Response.Write Err.Description Response.End End If

ASP技巧:ASP中三个常用语句的使用技巧

技巧|语句 一.On Error 语句该语句的作用是:启用或禁用错误处理程序.一般用法如下: On Error Resume NextOn Error GoTo 0 如果在您的代码中未使用On Error Resume Next语句,所发生的运行时错误将显示错误信息,同时,代码的执行也随之终止. 但当你采用它时,就会使程序按照产生错误的语句之后的语句继续执行,或是按照最近一次所调用的过程(该过程含有 On Error Resume Next 语句)中的语句继续运行.这个语句可以不顾运行时错误,继

《VBScript制作实例》笔记

vbscript|笔记  =========== 添加vbscript代码的三种方法 ============假设已有一个名为btnSubmit的按钮,添加vbscript代码的方法有三种: 第一种,最常用:<script language="VBScript">private sub btnSubmit_OnClickmsgbox("OK!")end sub</script> 第二种,使用For/Event属性:<script lan

QTP自动化测试之VBScript基础

要想使用QTP进行自动化测试,必须了解VBScript这门语言,对于使用过ASP或VB开发的人来说,VBScript已经再熟悉不过了,但是没有接触过VBScript的同学也不要灰心,因为这门语言简单易学. 1. VBScript利器  2. Hello World  3. 数据类型  4. 变量  5. 常数  6. 运算符 1. VBScript利器 子曰:工欲善其事,必先利其器.学习一门语言自然是离不开工具及文档,有好的工具及文档在手,学习起来也会得心应手.在此,我推荐大家一个很不错的编辑工

vbscript,jscript脚本编程教学(1)_vbs

by sssa2000 7/4/2004 论坛上好多朋友都问关于脚本的问题,正好最近对脚本比较有兴趣,就写点东西吧.首先说明一下,我的所有代码都是vbscript,jscript我没有研究过,不过我想也差不多. 关于最基础的语法比如变量的申明,分支,循环,函数的调用,等等这些我就不讲了,不懂得自己看一下. 1.我们的第一个vbs程序:还是那个老得掉牙的冬冬. ************************hello.vbs************************** dim hello

vbs/js脚本编程教学(1)_vbs

脚本编程教学(1)   论坛上好多朋友都问关于脚本的问题,正好最近对脚本比较有兴趣,就写点东西吧.首先说明一下,我的所有代码都是 vbscript,jscript我没有研究过,不过我想也差不多. 关于最基础的语法比如变量的申明,分支,循环,函数的调用,等等这些我就不讲了,不懂得自己看一下. 1.我们的第一个vbs程序:还是那个老得掉牙的冬冬. ************************hello.vbs************************** dim hello hello="