Link to home
Start Free TrialLog in
Avatar of shanemay
shanemayFlag for United States of America

asked on

ADO.Net error handling

I am looking for some advice or guidance on catching errors when working with ADO.net and SQL server.  Most examples and books, show code like

try
{
}
catch (Exception ex)
{
}

That works fine, however, I have never liked catching "Exception".  What is the best practice when catching exceptions in regard to ADO.Net?  
Avatar of guru_sami
guru_sami
Flag of United States of America image

you can catch provider specific exceptions like below:

try
{
  //Data access code
}
catch (SqlException sqlex) // more specific
{
}
catch (Exception ex) // less specific
{
}
finally{
 //if you are not having using construct don't forget to close connection and reader objects here
}

Check Exceptions handling here: http://msdn.microsoft.com/en-us/library/ee817654.aspx
Avatar of thepanch
thepanch

i suggest you to throw the exception on the main class and then in the view catch it like this

public class clAlumno
{
 public DataTable ObtenerDataTableReporteAspirante(long ID_Alumno)
    {
        DataTable dt;
       
        try
        {
            oCon.OpenConnection();
            oCon.clearParameters();
            oCon.addParameter("@ID_Alumno", ID_Alumno);
            dt = oCon.getDataTableSP("ProcRptFichaAspirante");          
        }
        catch (Exception ex) { throw new Exception(ex.Message); }
        finally { oCon.CloseConnection(); }
        return dt;
    }
}

and then in the view, only do the try catch
like the code you show

Catch exception only if you have a purpose to do something in the catch. For example:

1. Catch exception in the ui/form code - for the Purpose of: showing the error message for example in a msg box to the user.

UI Code:
private void buttonCreate_Click(...)
{
      try
      {
           da.CreateCustomer(....);
       }
       catch(Exception ex)
       {
            MessageBox.Show(ex.Message);
        }
}

2. Catch exception in non-ui code for the Purpose of: wrapping the exception in an application specific exception class and rethrow. Because there can be different types of exception in the lower layer (for example Data Access layer) and top ui layer can not expect all the types of exception; instead if the data access layer throws a common application specific custom exception (e.g. OurDataAccessException), then ui layer can specifically catch this exception.

Data Access Code:

public bool CreateCustomer(...)
{
    try
    {
          // some sql operation here
    }
    catch(SqlException sqlEx)
    {
         // wrap the SqlException in our custom exception so that higher layer code can specifically catch our exception, because higher layer sometimes wont know what are all the lower layer exceptions it can expect.
           
         throw new OurDataAccessException("Error creating customer...", sqlex );
     }

}

3. Catch exception anywhere for the purpose of logging the exception and rethrow the same exception.

    try
    {
          // some sql operation here
    }
    catch(Exception ex)
    {
         LogException(ex)
         
         throw ex; // we wanted only to log; rethrow the same exception
    }

If there is no specific purpose, then dont catch; let it go to the higher level code....

Avatar of Rahul Goel
You can use Enterprise Library in .Net Handling...and you can define the policies for the SQL Exceptions.
Using this framework you can wrap the SQL Exception or any unhandled exception into a custom Exception without wring single line of code.
http://entlib.codeplex.com 
 
ASKER CERTIFIED SOLUTION
Avatar of Naman Goel
Naman Goel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shanemay

ASKER

Thank you for your help.  It is greatly appreciated.