error handling??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have an object that I have defined but not initialized. I want to
initialize it in the try{} block, if every thing goes fine I want to return
this object.
What would I return in the catch block? as this function returns the object
type.
Whats wrong with this?

SqlDataReader myReader;
try
{
myreader=cmd.executereader(strquery,connectionobject);
return myreader;
}
catch
{
return myreader=null;
}
 
Hi,
Even if you do not set the SqlDataReader to null in the Catch block, it will
be returned as null as it would not have been initialised in the try block.

What you should finally return would actually depend on how the Caller of
this function handles the value returned by this function. If the caller
function can handle a Null value and treat it gracefully without crashing the
app. you can go ahead with this but if the caller cannot handle Nulls, it
might be a good idea to throw back an exception that can be your own app
specific exception that the Caller can handle and act appropriately.

-Ashish.
=====
 
Back
Top