error handling??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mavrick101

    error handling??

    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.ex ecutereader(str query,connectio nobject);
    return myreader;
    }
    catch
    {
    return myreader=null;
    }

  • Ashish Mathur

    #2
    RE: error handling??

    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.
    =====

    "mavrick101 " wrote:
    [color=blue]
    > 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.ex ecutereader(str query,connectio nobject);
    > return myreader;
    > }
    > catch
    > {
    > return myreader=null;
    > }
    >[/color]

    Comment

    Working...