仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2626|回复: 20
打印 上一主题 下一主题

[学习教程] ASP网站制作之Generating Sensible Error Messages U...

[复制链接]
谁可相欹 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:39:23 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
国内有些大的CRM厂商的ASP就写得不错.无论是概念还是它里面用JAVASCRIPT的能力.并不是说现在的程序员用了ASP.NET来写程序就可以说自己高档了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:&nbsp</p>  asp对于服务器的要求较高,一般的服务器如果访问量一大就垮了,不得不重启。
谁可相欹 该用户已被删除
沙发
 楼主| 发表于 2015-5-1 03:43:06 | 显示全部楼层
弱类型造成潜在的出错可能:尽管弱数据类型的编程语言使用起来回方便一些,但相对于它所造成的出错几率是远远得不偿失的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-19 20:22

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表