当前位置: 首页 > 图文教程 > 网络编程 > ASP > 处理Asp中的错误

ASP
论坛树状记录表的堆栈展开
Oracle大文本在ASP中存取问题的解决
ASP创建SQL Server数据库的两种方法
浅谈session_onend的调试
用ASP访问数据库的几种常见方式
教你一次下载网页中的所有资源
20种看asp源码的方法及工具
在ASP中使用智岛网格控件全过程
解决使用ASP无法连接ORACLE 9i数据库的问题
用Js判断输入的时间是否有效
ADO数据与XML数据间的转换的类
使用命名管道访问SQL Server
网页加速之网页结构篇
在红蜻蜓中实现多房间
在ASP中使用Oracle数据库技巧
关于分页办法
在VB中使用API函数(Any)
网站ASP漏洞小总结
网页之定时器详解
实用函数- 判断输入的是否是电话号码

ASP 中的 处理Asp中的错误


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 32 ::
收藏到网摘: n/a

       处理Asp中的错误
  
  You just finished your ASP application in time, barely making the dealine! As you upload the final version to the webserver, you receive an email from your supervisor indicating that he''s ready for your demo. You confidently stride into his office and give him instructions on accesses the application through the Intranet.
  
  Your boss comes to the first page, and is asked to enter his name. You''ve created a slick, customized feel to the site, recording the user and his or her preferences in a database. Your boss types in his name, "The Pointy Haired Boss," and hits the submit button. Instead of being taken to the next page, he sees the following text pop up on the screen:
  
  Microsoft OLE DB Provider for ODBC Drivers error '' 80004005'' [Microsoft][ODBC Access 97 ODBC driver Driver] syntax wrong near ''
  
  Please note that I just made this error message up, so, please, no emails asking about how one gets this error by entering his name!
  
  You get one of those looks from your boss, a look that says, "I hope you weren''t expecting a positive performance review."
  
  There''s nothing worse to an end user than to see those dreaded ADO errors. They are misformatted, cryptic, and leave the user wondering what they are suppose to do. Your average end user doesn''t know ODBC from OLE. So how can you prevent these nasty error messages?
  
  First you must insert at the top of your ASP page:
  
  
  <% On Error Resume Next %>
  This will tell ASP to just skip over any errors, instead of halting execution and printing out a nasty error message. You may be tempted to leave it at this, although if you do, and errors occur, your users won''t be aware of them. They will think their information was correctly saved, when in actuality it wasn''t due to an ADO error.
  
  What, then, do we want to do if there is an error? Well, the idea situation would be to let the end user know that an error had occurred and will be worked on, and automatically email technical support with the ADO error messages. So that is what we will do!
  
  The question now is how to we "trap" errors? Well, after any ADO call that accesses the database, we will want to put the following lines:
  
  
  
  If Err.number <> 0 then
   TrapError Err.description
  End If
  
  You will want to put this after all ADO calls that communicate directly with the database. This includes ConnectionObject.Open, ConnectionObject.Execute, and RecordsetObject.Open. Now, you may be wondering where the sub TrapError is defined: we''re about to do that. Create a file called ErrorHandler.asp and put it in your /include or /scripts directory. In ErrorHandler.asp, we will have the following subs:
  
  
  TrapError
  ProcessErrors
  Let''s look at the code for ErrorHandler.asp:
  
  
  
  <%
  Dim strErrorMessage
  Dim bolErrors
  
  ''Initialize variables
  strErrorMessage = "" ''The error messages for tech. support
  bolErrors = False ''Have we found any errors yet?
  
  ''Now our two subs
  sub TrapError(strError)
   bolErrors = True ''Egad, we''ve found an error!
  
   strErrorMessage = strErrorMessage & strError & ", "
  end sub
  
  
  
  ''If there are any errors, this function will email tech. support
  sub ProcessErrors()
   if bo