asp.net的错误处理机制讲解
2023-06-13 09:13:55 时间
程序健壮性最基本要求就是程序错误的处理与捕捉,在ASP.NET中,错误的处理有和其他编程语言一样的机制,可以使用Try…Catch…Finally等方式,这一点和ASP相比具有较大的进步。而且,使用这些错误处理方法,可以大大提高程序的可读性和程序调试速度,在这几个优势结合的情况下,我们更加应该注意这一点。
关于错误的处理,我们可以参考这篇文章:
Try...Catch...Finally in ASP.NET
Introduction
Error handling in Classic ASP was not the best. We were having only limited options available for error handling in Classic ASP such as, "On Error Resume Next". In ASP 3.0 we saw the new ASP object called Error Object. But we were not able to handle all exception/errors efficiently. Now in ASP.NET we have a new error handling mechanism which was already their in other languages such as C, C++ and JAVA. We can also call the try...catch mechanism as "Exception Handling"
What is Try...Catch....Finally
This is a new error handling mechanism in VB.NET, so as in ASP.NET. Well we have three blocks of code, were each block has it own functionality. The Try...Catch...Finally block of code surrounds the code where an exception might occur. The simple Try statement comes before the block of code, the Catch block of code is where we specify what type of error to look for, and the Finally block of code is always executed and contains cleanup routines for exception situations. Since the catch block is specific to the type of error we want to catch, we will often use multiple Catch blocks in our Try...Catch...Finally structure.
A simple Database operation
Dim mySqlConnection as New SqlConnection (ConnectionString)
Dim mySqlCommand as SqlCommand
Dim strSql as String
strSql = "insert into yourtable (f1, f2) values ("f1", "f2")"
mySqlCommand = new SqlCommand(strSql, mySqlConnection)
Try
mySqlConnection.Open()
mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
Message.text = "New Forward information added"
Catch SQLexc as sqlexception
Message.text = Message.text + sqlexc.tostring()
Catch exc as exception
if Instr(1, exc.tostring, "duplicate key") > 0 then
Message.text = Message.text + "Cannot insert duplicate values."
else
Message.text = Message.text + exc.tostring()
end if
Finally
mySqlConnection.Close()
End Try
What does the above example exactly do?
Well, in the above example we were trying to insert some values to a database table. The possible chances while performing a database operation are invalid connection string, database server too busy resulting in connection time out, database server not currently running etc etc. We should anticipate all these errors while performing a database operation. So, we have a Try block, which contains the statements such as opening the connection and executing the operation. Basically, we have two major statements inside the try block which may result in an exception/error.
As I said, any exception can occur during a database operation. Catching all these exception is now very easy with the Catch block. All we need is to have a Catch block. We can have any number of Catch blocks. Each Catch block may have a different error/exception trapping mechanism. In the above example, we have two catch blocks, one which captures a general exception and the other one which traps the SqlException.
When all the statements inside the catch blocks are executed, the finally block comes into the picture. As I said earlier, finally block contains cleanup routines for exception situations.
Exit Try statement
We can also have the Exit Try statement inside any of the try...catch block. The objective of this statement is to break out of the Try or Catch block. Once the Exit Try statement is executed, the control goes to the Finally block. So, Exit Try statement can be best used were we need to execute the cleanup routines.
How about nested Try statments?
We can have nested Try and Catch blocks. Can you imagine, when we should use nested try statements. Well, errors can occur within the Catch portion of the Try structures, and cause further exception to occur. The ability to nest try structures is available so that we can use a second Try structure to cover exceptions.
Links
http://www.vbweb.co.uk/show/1889/2/ http://www.oreillynet.com/pub/a/dotnet/2001/09/04/error_handling.html?page=2
关于错误的处理,我们可以参考这篇文章:
Try...Catch...Finally in ASP.NET
Introduction
Error handling in Classic ASP was not the best. We were having only limited options available for error handling in Classic ASP such as, "On Error Resume Next". In ASP 3.0 we saw the new ASP object called Error Object. But we were not able to handle all exception/errors efficiently. Now in ASP.NET we have a new error handling mechanism which was already their in other languages such as C, C++ and JAVA. We can also call the try...catch mechanism as "Exception Handling"
What is Try...Catch....Finally
This is a new error handling mechanism in VB.NET, so as in ASP.NET. Well we have three blocks of code, were each block has it own functionality. The Try...Catch...Finally block of code surrounds the code where an exception might occur. The simple Try statement comes before the block of code, the Catch block of code is where we specify what type of error to look for, and the Finally block of code is always executed and contains cleanup routines for exception situations. Since the catch block is specific to the type of error we want to catch, we will often use multiple Catch blocks in our Try...Catch...Finally structure.
A simple Database operation
Dim mySqlConnection as New SqlConnection (ConnectionString)
Dim mySqlCommand as SqlCommand
Dim strSql as String
strSql = "insert into yourtable (f1, f2) values ("f1", "f2")"
mySqlCommand = new SqlCommand(strSql, mySqlConnection)
Try
mySqlConnection.Open()
mySqlCommand.ExecuteReader(CommandBehavior.CloseConnection)
Message.text = "New Forward information added"
Catch SQLexc as sqlexception
Message.text = Message.text + sqlexc.tostring()
Catch exc as exception
if Instr(1, exc.tostring, "duplicate key") > 0 then
Message.text = Message.text + "Cannot insert duplicate values."
else
Message.text = Message.text + exc.tostring()
end if
Finally
mySqlConnection.Close()
End Try
What does the above example exactly do?
Well, in the above example we were trying to insert some values to a database table. The possible chances while performing a database operation are invalid connection string, database server too busy resulting in connection time out, database server not currently running etc etc. We should anticipate all these errors while performing a database operation. So, we have a Try block, which contains the statements such as opening the connection and executing the operation. Basically, we have two major statements inside the try block which may result in an exception/error.
As I said, any exception can occur during a database operation. Catching all these exception is now very easy with the Catch block. All we need is to have a Catch block. We can have any number of Catch blocks. Each Catch block may have a different error/exception trapping mechanism. In the above example, we have two catch blocks, one which captures a general exception and the other one which traps the SqlException.
When all the statements inside the catch blocks are executed, the finally block comes into the picture. As I said earlier, finally block contains cleanup routines for exception situations.
Exit Try statement
We can also have the Exit Try statement inside any of the try...catch block. The objective of this statement is to break out of the Try or Catch block. Once the Exit Try statement is executed, the control goes to the Finally block. So, Exit Try statement can be best used were we need to execute the cleanup routines.
How about nested Try statments?
We can have nested Try and Catch blocks. Can you imagine, when we should use nested try statements. Well, errors can occur within the Catch portion of the Try structures, and cause further exception to occur. The ability to nest try structures is available so that we can use a second Try structure to cover exceptions.
Links
http://www.vbweb.co.uk/show/1889/2/ http://www.oreillynet.com/pub/a/dotnet/2001/09/04/error_handling.html?page=2
相关文章
- 可用免费asp.net空间
- [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 2.0 Web.UI「建议收藏」
- .net 温故知新:【8】.NET 中的配置从xml转向json
- .Net Core3.1 SignalR for WPF Asp.net
- asp.net的web服务MSSQL检测ASP.NET的Web服务——利用它构建更棒的站点(mssql检测基于)
- ASP.Net全局变量的设置和读取方法
- asp下连接数据库ASP链接数据库字符串大全总结
- ASP.NET多次提交的解决办法
- ASP.NET多次提交的解决办法2
- asp.net使用for循环实现Datalist的分列显示功能
- 一天精通asp.net的学习经验小结
- asp.net处理原文件中过长的viewstate代码
- asp.net下Repeater使用AspNetPager分页控件
- asp页面和Asp.net页面传中文参数UrlEncode编码以及接收解码
- 写给初学asp.net的新人们新手学习经验
- asp.net通过AjaxUpdatePanel回传后滚动条位置变更解决方法
- ASP.NET实现word文档在线预览功能代码
- Jquery+ajax请求data显示在GridView上(asp.net)
- 答你所问.NET小常识方便学习asp.net的朋友
- 浅析ASP.NET安全性分析(加强asp.net1.1/2.0安全性)
- asp.net中session的原理及应用详解
- 使用.NET中的Action及Func泛型委托深入剖析
- ASP.NET获取MSSQLServer安装实例实现思路及代码
- 基于ASP.NET的lucene.net全文搜索实现步骤
- .Net中导出数据到Excel(asp.net和winform程序中)
- asp.net实现的计算网页下载速度的代码
- 深入Lumisoft.NET实现邮件发送功能的方法详解
- asp.net获取服务器基本信息的方法代码
- asp.net中C#获取字符串中汉字的个数的具体实现方法
- asp.net利用后台实现直接生成html分页的方法
- ASP.NET中使用TreeView显示文件的方法