asp错误的几种处理方式(2)


%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="#C0C0C0">
<FONT FACE="ARIAL">An error occurred in the execution of this ASP page<BR>
Please report the following information to the support desk<P>
<B>Page Error Object</B><BR>
错误 Number: <%= Err.Number %><BR>
错误信息: <%= Err.Description %><BR> 
出错文件: <%= Err.Source %><BR>
出错行: <%= Err.Line %><BR>
</FONT>
</BODY>
</HTML>

<%End If%>


你们上面看到了,我首先设置On Error Resume Next ,这样出现错误就不会影响程序的执行。

错误处理和数据库
在错误处理中加入数据库的执行是很复杂的。假若我们有一个程序,有很多的命令去向数据库中添加记录,如果insert/update在程序的最底部执行,如果我们前面又错误发生,那就完了!我们就会向数据库中添加了一个错误的信息。因为我们用了On Error Resume Next 一切的错误都被忽略了!即使前面出错,程序依旧会向数据库中添加数据的。
为避免这种情况,我们就先得做些手脚,正确处理的方法如下: 

If Err.Number = 0 And objConnection.Errors.Count = 0 Then

'这里才能执行语句,因为没有错误
Set rstResults = dbData.Execute(txtSql)

End If



更多高级的处理办法
当一个错误发生时,你们也可以显示更多的错误信息。下面是同时处理数据库和页面错误的例子,有了它我们一下就能发现我们程序中的所有错误。(由于有些地方我觉得英文更能说时问题,所以没有翻译)。 
<% 
If Err.Number <> 0 Then
Response.Clear
Select Case Err.Number
Case 8 '指定错误的Number
'在这里处理自定义错误 

Case Else '一般错误

If IsObject(objConnection) Then
If objConnection.Errors.Count > 0 Then
%>

<B>Database Connection Object</B>

<% For intLoop = 0 To objConnection.Errors.Count - 1 %>

Error No: <%= objConnection.Errors(intLoop).Number %><BR>
Description: <%= objConnection.Errors(intLoop).Description %><BR>
Source: <%= objConnection.Errors(intLoop).Source %><BR>
SQLState: <%= objConnection.Errors(intLoop).SQLState %><BR>
NativeError: <%= objConnection.Errors(intLoop).NativeError %><P>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3181.html