Error Okay, I ' ll admit it, if there ' 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 On. (For more information on the ERR object is 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 has 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 WR Ong 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 so works.
Now consider a script like this one, 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 to 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 are 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 has 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 the does exactly this. The Raise method has the following syntax:
The technical docs for the Raise can is seen here. A quick Note:when raising a custom error, the error number should be a custom error number plus the constant Vbobjecterro R, to make sure this error number you choose doesn ' t equal a already error number (we LL This in the code below).
An example of using the Raise object to generate we own custom error (with a more descriptive 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, _
: "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:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.