0

How could I use a try catch function over here?

public DataTable BindRole()
{  
    Database _database = DatabaseFactory.CreateDatabase();
    DbCommand dbCommand = _database.GetSqlStringCommand(QMROLE.FetchData);
    DataSet _ds = _database.ExecuteDataSet(dbCommand);
    return _ds.Tables[0];      
}

Thanks.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Arshad Ahmed
  • 35
  • 3
  • 9

6 Answers6

2
public DataTable BindRole() 
{
    try  
    {  
        Database _database = DatabaseFactory.CreateDatabase();  
        DbCommand dbCommand = _database.GetSqlStringCommand(QMROLE.FetchData);  
        DataSet _ds = _database.ExecuteDataSet(dbCommand);  
        return _ds.Tables[0];    
    }  
    catch (Exception ex)  
    {  
        return null;  
    }
}
1

When a function returning any value then after catching exception return null which would be helpful as shown below.

public DataTable BindRole()
{
   try
   {
       Database _database = DatabaseFactory.CreateDatabase();
       DbCommand dbCommand = _database.GetSqlStringCommand(QMROLE.FetchData);
       DataSet _ds = _database.ExecuteDataSet(dbCommand);
       return _ds.Tables[0];
   }
   catch
   {
       return null;
   }
}

Now when using this method.

Datatable resultantTable = BindRole();
if ( resultantTable != null )
{
     // Do what you want with this datatable
}
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
1

I prefer to go with the following. You use 'using' for the objects which implement the IDisposable interface. That ensures, that after their use, and on error, their ressources are released. I do not know the Database object, but if it also implements IDisposable, then use 'using', otherwise just use the surrounding try-catch, and perform cleanup tasks in the finally. I usually chose to create the return variable on top of a method with their default value, and return it at the end. So you always get the actual state of the variable at the end.

public DataTable BindRole()
{
    DataTable dataTable = new DataTable();

    try
    {
        Database _database = DatabaseFactory.CreateDatabase();

        using (DbCommand dbCommand = _database.GetSqlStringCommand(QMROLE.FetchData))
        {
            using (DataSet _ds = _database.ExecuteDataSet(dbCommand))
            {
                dataTable = _ds.Tables[0];
            }
        }
    }
    catch
    {
    }
    finally
    {
        // perform cleanup
    }

    return dataTable;
}
Kai Hartmann
  • 3,106
  • 1
  • 31
  • 45
  • You do not handle the exception. Only catch exceptions when you can handle them. For the cleanup, you can still keep the `finally` block. – Myrtle Sep 25 '13 at 07:38
  • It would depend on what the OP needs. This implementation ensures, that all error handling is performed in this method, and any usage of it would either result in a DataTable-object or null. But you are right, if the error is of any relevance outside of this method, then the catch should be removed. – Kai Hartmann Sep 25 '13 at 07:49
  • I do not agree. If it is expected for the database connection to fail, a empty dataTable is accepted. In every other case, it is an exception and it is best practise to handle exceptions only when you can. If you actually do decide to catch them, at least write a trace. – Myrtle Sep 25 '13 at 08:05
  • I agree on that 'empty DataTable' part, and updated the answer accordingly, since it would not require the method caller to check for null. – Kai Hartmann Sep 25 '13 at 08:28
  • FYI, the subject matter of returning null vs. an empty object is discussed in detail here: http://stackoverflow.com/questions/1626597/should-functions-return-null-or-an-empty-object – Kai Hartmann Sep 25 '13 at 08:37
0

use "throw or "return null" or return a new empty object. I recommend throw, except you handle the exception right in the catchblock (f.e log it). Do not catch the exception and rethrow it (unless you have your reasons)

catch (Exception error) { throw error;} 

cause in this case the original stacktrace is lost.

public DataTable BindRole()
 {
   try
    {
       Database _database = DatabaseFactory.CreateDatabase();
       DbCommand dbCommand = _database.GetSqlStringCommand(QMROLE.FetchData);
       DataSet _ds = _database.ExecuteDataSet(dbCommand);
       return _ds.Tables[0];
    }
   catch
    {
       throw;
    }
}
BudBrot
  • 1,341
  • 2
  • 24
  • 44
  • Yes, but the question was "how to use Try\catch blocks in returnmethods". I thought he had the problem, that he have to return also a value - or like i did rethow an exception- to get clean code which is executable. – BudBrot Sep 25 '13 at 08:29
0

Your question is rather vague. You could just wrap the whole thing in a TryCatch. Or individual commands, depending on your need.

Or are you asking what should be done in the catch to clean up if there is an error in one of the commands?

If you give us a hint as to what you are trying to achieve, other than catching an error (which can be done by putting the try/catch around the entire block), let us know.

JustMeToo
  • 325
  • 1
  • 14
0

You can use like this

public DataTable BindRole()
{  
   DataSet _ds = new DataSet();
   try
   {
    Database _database = DatabaseFactory.CreateDatabase();
    DbCommand dbCommand = _database.GetSqlStringCommand(QMROLE.FetchData);
    DataSet _ds = _database.ExecuteDataSet(dbCommand);    
    return _ds.Tables[0];
   }
   catch(Exeption Ex)
   {
    // either trow the exception or 
    // return an empty datatable here
    return new DataTable();
   }       
}

In you calling function check for empty datatable in case of any error.

nrsharma
  • 2,532
  • 3
  • 20
  • 36
  • if _ds.Tables[0]; does not exist this will raise an (maybe unhandled) exception. I recommend to retrun it in the tryblock – BudBrot Sep 25 '13 at 06:42