views:

524

answers:

4

In an attempt to add some parameter validation and correct usage semantics to our application, we are trying to add correct exception handling to our .NET applications.

My question is this: When throwing exceptions in ADO.NET if a particular query returns no data or the data could not be found, what type of exception should I use?

Psuedocode: (read, don't scrutinize the semantics of the code, I know it won't compile)

public DataSet GetData(int identifier)
{
    dataAdapter.Command.Text = "Select * from table1 Where ident = " + identifier.toString();
    DataSet ds = dataAdapter.Fill(ds);
    if (ds.table1.Rows.Count == 0)
        throw new Exception("Data not found");

    return ds;
}
+2  A: 

As far as ADO.net is concerned, a query that returns zero rows is not an error. If your application wishes to treat such a query as an error, you should create your own exception class by inheriting from Exception.

public class myException : Exception
{
   public myException(string s) : base() 
   {
      this.MyReasonMessage = s;
   }
}

public void GetData(int identifier)
{
    dataAdapter.Command.Text = "Select * from table1 Where ident = " + identifier.toString();
    DataSet ds = dataAdapter.Fill(ds);
    if (ds.table1.Rows.Count == 0)
        throw new myException("Data not found");
}
Richard Yorkshire
Inherit from ApplicationException, the Microsoft-recommended class for deriving custom exceptions.
icelava
+2  A: 

You really should define your own exception : DataNotFoundException.

You should not use the basic class Exception, since when you will catch it in calling code, you will write something like

try
{
     int i;
     GetData(i);

}
catch(Exception e) //will catch many many exceptions
{
    //Handle gracefully the "Data not Found" case;
    //Whatever else happens will get caught and ignored
}

Where as catching only your DataNotFoundEXception will get only the case you really want to handle.

try
{
     int i;
     GetData(i);

}
catch(DataNotFoundException e) 
{
    //Handle gracefully the "Data not Found" case;
} //Any other exception will bubble up

There is a class aptly named SqlException, when there are troubles with the SQL engine but it's better not overload it with your business logic

Johan Buret
+6  A: 

The MSDN guidelines state:

  • Consider throwing existing exceptions residing in the System namespaces instead of creating custom exception types.

  • Do create and throw custom exceptions if you have an error condition that can be programmatically handled in a different way than any other existing exceptions. Otherwise, throw one of the existing exceptions.

  • Do not create and throw new exceptions just to have your team's exception.

There is no hard and fast rule: but if you have a scenario for treating this exception differently, consider creating a custom exception type, such as DataNotFoundException as suggested by Johan Buret.

Otherwise you might consider throwing one of the existing exception types, such as System.Data.DataException or possibly even System.Collections.Generic.KeyNotFoundException.

Joe
A: 

just a bit note: IF POSSIBLE,consider possibility don't use exceptions,but error codes instead. this will increase youre performance.

stefano m
It's rare that the performance hit of using exceptions outweighs their benefit. Unless the application is a truely real time environment (unlikely given the context of the question), Exceptions are a much better way of ensuring your software is reliable.
Ian Gregory