Question: I have a method that returns a `List`. Now I want to know how to place the `try/catch` blocks properly. If I place the `return` statement inside `try` I get error

> 
> Not all code paths return a value
> 
> 
> 

If I place after `catch`(like I'm doing currently) it will return the `products` even after an `Exception`. So what should be the best way?

Here is the method:

```
public List<Product> GetProductDetails(int productKey)
{
    List<Product> products = new List<Product>();
    try
    {
       using (SqlConnection con = new SqlConnection(_connectionString))
       {
         SqlCommand cmd = new SqlCommand("usp_Get_ProductDescription", con);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.AddWithValue("@riProductID", productKey);
         con.Open();
         using (SqlDataReader reader = cmd.ExecuteReader())
         {
           while (reader.Read())
           {
             Product product = new Product(reader["Name"].ToString(), reader["Code"].ToString());
             products.Add(product);
           }
         }
       }
     }
     catch { }
     return products;
}

```

Answer: 
Remove the complete `Try` and `Catch` blocks. Apparently you are unable to handle the exceptions in the `GetProductDetails` method so just let them be thrown.

However the calling code can make the decision:

```
IList<Product> products = null;

try
{
    products = GetProductDetails(3);
}
catch(Exception ex)
{
    // Here you can make the decision whether you accept an empty list in case of retrieval errors.
    // It is the concern of this method, not of the ProductDetails method.
    // TODO: Use logging
    products = new List<Product>();
}

```

I can imagine it feels like code duplication if you have to write this in every method using the `GetProductDetails` method. However consider, when you have X implementations, you want to react differently to not being able to get product details. You will have to come up with workarounds. You might even end up with weird bugs which are hard to troubleshoot.
At the moment, you don't return anything if an exception is thrown.
Use `try, catch, finally`. (May you want to see the [MSDN page](https://msdn.microsoft.com/en-us/library/dszsf989.aspx) for more official information)

```
try
{
    //try to execute this code
}
catch
{
    //execute this if an exception is thrown
}
finally
{
    //execute this code, after try/catch
}

```

So if you place your `return` statement into the `finally` section, you will return your list even if there's an exception thrown...