tags:

views:

103

answers:

4

I have a CustomerRepository class (in my BL), and I am returning a collection as follows:

 public static ICollection<Customer> FindCustomers()
    {
        Collection<Customer> customers = null;
        try
        {
           customers = DAL.GetCustomers();             
        }
        catch (Exception ex)
        {
            //log and re-throw exception here
        }
        return customers;
    }

I have a few questions on this:

  1. Is the try/catch block ok?
  2. I am creating the collection outside try, and returning it outside catch.

Am I overlooking any best practices here?

Would love to know about potential gotchas here :)

+3  A: 

This is fine (and idiomatic)

public static ICollection<Customer> FindCustomers()
{
    try
    {
       return DAL.GetCustomers();         
    }
    catch (Exception ex)
    {
        //log and re-throw exception here
    }
}

I would add that returning IQueryable (or if not feasible IEnumerable) is probably a better idea so as to give your class more wiggle room in future as to how the data is arranged.

ShuggyCoUk
+1 for the IQueryable/IEnumerable
Ash M
+3  A: 
public static ICollection<Customer> FindCustomers()
{
        try
        {
           return DAL.GetCustomers();
        }
        catch (Exception ex)
        {
            //log here
            throw;
        }
}

I think this is better version

ArsenMkrt
A: 

What would happen if suppose an error comes in try block before return statement, as in below code i am manually throwing a exception and compiler is warning me at return h; line that it is unreachable code.

 public int Test()
        {
            try
            {
                int h = 0;
                h = 100;
                throw new Exception();
                return h;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

Is it ok to have such warnings?

Raghav Khunger
The best practice would be to have the int h; outside the try, h=0 inside the try and return h; outside the try. Why would you want to ignore the warning if you can take care of it?
Rashmi Pandit
When I said h=0 should be inside the try, I was thinking of complex data types where there is a good chance that the assignment statement can throw an exception. (Considering h is int, you don't need to assign 0 as it is the default value for int anyways.)
Rashmi Pandit
@Rashmi: That is my question: initially my collection was outside try, but all answers take the collection inside the try block and return it from there. So which approach is best?
Raghav Khunger
@rashmi: I am sorry I think I should have specified it clearly that int h=0 and the line below it means some processing is going on before returning the value.
Raghav Khunger
A: 

If you are doing some processing inside try, then declare your returning objects outside try and return them outside catch. So I think what you wrote is correct.

I think if you use more specific interfaces (like IEnumerable<>) then your consumers (or upper layers/tiers) might have problems using your collection classes. They might need to add more work (like IEnumerable does not support the Count property). Using ICollection<> or even Collection<> should also be ok.

Vivek