The best and common coding technique for catching and handling the Errors and Exceptions in ASP.Net for specific ADO.Net Data Providers is using try… catch … finally block. In try …catch block, try section is used to cover the piece of code that can throw an error or exception and catch block is used to catch that error or exception using base Exception object or code specific Exception handler. E.g. SqlException class object is used to handle the SQL Server .Net Data Provider in ADO.Net data access coding.
try { // ADO.Net Data Access Code here } catch(SqlException sqlEx) { // handle sql specific errors // to display more specific error message } catch(Exception ex) { // Base Exception class // It displays less specific error message // for ADO.Net Data Access Code }
When you are using more than 1 catch block like in above example code, always remember to set the order of catch block from more specific exception handling to less specific block. This enables you to handle and display the more specific error messages according to the execution order of try …catch block.
SqlException class provides the following properties for any error or exception handled by it:
Currently rated 5.0 by 1 people
Tags: asp.net, ado.net, error and exception handling, database connection, connection pooling, c# code
7/24/2008 5:23:33 PM