-1

Here my code. Can anyone tell where to put the return statement? I'm using a 3 tier concept.

DataTable obj_dt = new DataTable();
public DataTable City_Name_get()
{            
    try
    {
        string store_pro = "sp_tb_city";
        obj_DB_Con.connection_db(store_pro, null);
        return obj_dt;
    }      
    catch (Exception ee)
    {
        MessageBox.Show(ee.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
psubsee2003
  • 8,563
  • 8
  • 61
  • 79
Jana
  • 49
  • 3
  • 8
  • Example is not complete, so it's hard to say. I would use try-catch block and put return statement after catch block. In catch block i would throw an exception to upward towards UI and decide at method calling end what to do with exception (display messagebox, log, etc.) – Panu Oksala Dec 07 '13 at 10:15
  • 1
    Just don't swallow the exception (that is: get rid of that try-catch construct) and you'll be ok. – Sjoerd Dec 07 '13 at 10:18
  • Now your code has only one return statement inside of try-block which is fine. But in case of error the error will be catched in the catch-block and so the first return can't be reached. Therefore you have to add some additional return to catch-block as well. Or you could put throw statement inside of the catch-block. Or you could just display the message-box with the error like you are doing now but then you have to put retun statement after the catch block as well and not only inside of try-block. HTH – Daniel Dušek Dec 07 '13 at 10:46
  • You have to think about (include in your design) what should happen after an error. Putting up a messagebox is not good enough as an answer. – H H Dec 07 '13 at 11:21

3 Answers3

1

finally won't allow you to add return statement. Take a look at this to see why it is not legal to add control transfer statements in finally

You'll have to either put it outside of try/catch/finally completely or in both try and catch blocks else compiler won't be happy to compile.

public DataTable City_Name_get()
{
    try
    {
        string store_pro = "sp_tb_city";
        obj_DB_Con.connection_db(store_pro, null);
        return obj_dt;
    }      
    catch (Exception ee)
    {
        MessageBox.Show(ee.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return null;//Or whatever
    }
}

Or

public DataTable City_Name_get()
{
    try
    {
       //Do something
    }      
    catch (Exception ee)
    {
       //Do something more
    }
   finally
   {
      //Do something more
   }      
   return someDataTable;
}
Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
1

First of all, you should not really declare and initialise the return object outside of you method. I moved it inside in the code below.

As for returning, in case of exception you could return null (as you are handling the exception inside the method). This way the caller would know that there was an error. You should document that behaviour of the method.

Finally is not the place to return anything. It is there to run the code that always has to be run, even in case of exception, e.g. to close unmanaged resources.

public DataTable City_Name_get()
{
    DataTable obj_dt = new DataTable();
    try
    {
        string store_pro = "sp_tb_city";
        obj_DB_Con.connection_db(store_pro, null);
        return obj_dt;
    }      
    catch (Exception ee)
    {
        MessageBox.Show(ee.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return null;
    }
}
Szymon
  • 42,577
  • 16
  • 96
  • 114
1

It's a matter of personal taste. The catch block will return from the method after execution, and a finally block will execute right before the return statement anyway, regardless of its location. You may stick to one style or the other, but it should be consistent (when a team-wide style guide is in place, then this is the place to define it).

(Me personally, I'm writing the return always at the very end of the method. To me, this makes a somewhat better readability.)

HTH Thomas

Thomas Weller
  • 11,631
  • 3
  • 26
  • 34