I have code like this:
try {
if (!(oc.State == ConnectionState.Open)) {
oc.Open();
}
query = "SELECT DUCKBILL FROM PLATYPUS";
da = new OracleDataAdapter();
oCommand = new OracleCommand(query, oc);
oCommand.Parameters.Add("ABCid", platypusABCID);
da.SelectCommand = oCommand;
dt = new OracleDataTable();
da.Fill(dt);
return dt;
} catch (OracleException e) {
log.Error(e, e);
//return dt; //use of unassigned local variable 'dt'
}
//return dt; //use of unassigned local variable 'dt'
…which stops me with either “not all code paths return a value” or (as commented) “use of unassigned local variable ‘dt'”
I can get rid of the try..catch, but as this is a database operation, I’d like to be able to log any resultant err msg. How can I keep my exception handling and also appease the grouch? I know, it’s a benevolent grouch, but still…
Preassign it with a new table or null;
OR
And then:
If you assign it with a new table, it will return an empty table in case of an exception.
Choose which one you prefer logically, considering your situation.