热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

asp.net的错误处理机制讲解

asp.net的错误处理机制讲解
程序健壮性最基本要求就是程序错误的处理与捕捉,在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

推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 原文地址:https:www.cnblogs.combaoyipSpringBoot_YML.html1.在springboot中,有两种配置文件,一种 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
author-avatar
手机用户敬怡
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有