以国内最流行asp为例,我不知道有多少人会在写代码时想到“容错”这个概念,实际上当我遇到这种事时,也是不了了之。为什么呢,想想最初的意思是认为写如下代码就能容错了,见示例1-1。
<%@ language=vbscript %>
<%option explicit%>
<%
'出错过滤
on error resume next
……………(代码略)
%>
示例1-1 常见代码一瞥
以上代码就经常出现在各位同仁的手中,不用说出个中原因,我完成能理解各位现在的心情,我可以向大家坦白地说,我写了二年的asp网页,大多数都是这种方式,不断的写,不断的改,心力交瘁,现在都不愿回头改写自已的代码了。实际上最基本的容错机制的想法,就是不要相信程序能为你节省多少事,要把控制权掌握在自已的手中,这是必要的。
<%@ language=vbscript %>
<%option explicit%>
<%
'========================================
dim ndebug_msg
ndebug_msg=true
if ndebug_msg = true then
'出错过滤
on error resume next
end if
'===============end =====================
……………(代码略)
'数据事务处理-start
if err.number = 0 then
'~~~~~ open database and begin transaction ~~~~~~~~~~~~~~~~~~~~
'------------------------------
'-------------------------------------
objconn.begintrans
objconn.execute (objsql)
'~~~~~~ commit the transaction and close the database connection
objconn.committrans
response.cachecontrol ="private"
response.expires = -1
……………(代码略)
else
'~~~~~ rollback transactions and close objects
objconn.rollbacktrans
'~~~~~ raise errors for asp page
'err.raise err.number, err.source, err.description
'err.clear
response.write "description= ("& err.number &"),("& err.description &")"
end if
'==============end files=====================
%>
示例1-2 完整的容错机制代码范例
看了以上代码,你会发现也没有什么高超技艺在里面,高手可能更是不屑一顾。但大家未必能在写代码时这样作。
【1-1】编写动态网页时,一定要考虑容错机制,如在asp中,就应该参考示例1-2,写出健壮的代码。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器