Well, I got your point, but I do not agree with your point that my code is not good in programming point of view.
According to Microsoft, it says, you should only put try/catch when you can handle the exception. If you cannot handle the exception, you should not be handling the exception using try/catch, rather it should automatically throw the exception to the calling environment. So, as far this note is concerned anything as replied by Hetalkumar1980 or Gokul does not make any sense to me. So according to me, the code should look like either :
public DataSet GetOffers()
{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
using (da)
{
da.Fill(ds);
}
return ds;
}
without any try catch around, and letting the caller to handle the exception in the method or
public DataSet GetOffers()
{
try
{
string sqlQuery = "select OfferID,ProductName,ProductModel,Cost,OfferPrice from Offers";
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, con);
DataSet ds = new DataSet();
using (da)
{
da.Fill(ds);
}
return ds;
}
catch (Exception ex)
{
// Handle your exception . (say log exception or something else)
return null;
}
}
which is my previous solution, which intentionally returns null after handling exception (rather than rethrow the exception when exception occurs, which doesnt makes sense as we could easily get rid of the try catch for that)
I hope you will also agree with me.
www.abhisheksur.com
Agk484, if this helps please login to Mark As Answer. | Alert Moderator