Reputation: 35
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.
Upvotes: 0
Views: 3928
Reputation: 3154
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;
}
Upvotes: 1
Reputation:
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;
}
}
Upvotes: 2
Reputation: 3781
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
}
Upvotes: 1
Reputation: 2562
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.
Upvotes: 0
Reputation: 1411
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;
}
}
Upvotes: 0
Reputation: 425
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.
Upvotes: 0