interesting return issue in catch block.

Last post 11-20-2007 8:17 PM by Allen Chen – MSFT. 7 replies.

Sort Posts:

  • interesting return issue in catch block.

    11-19-2007, 2:37 PM
    • Member
      288 point Member
    • sahajMarg
    • Member since 05-03-2007, 6:59 PM
    • Posts 583

    Hello,

    In the following example, I don't want to rethrow an exception, but if I don't rethrow the exception i get an error message "not all code return a value"

    what should i do, I don't want to rethrow an exception.

      public string testme()
        {
            string hello = "hello";
            try
            {
                hello = "myhello";
                return hello;

            }
            catch(Exception ex)
            {
              
            }
        }

    kindly advice. Thanks

  • Re: interesting return issue in catch block.

    11-19-2007, 3:28 PM
    • Star
      12,508 point Star
    • Freakyuno
    • Member since 01-20-2005, 4:57 PM
    • Midwest - United States
    • Posts 1,952
    • TrustedFriends-MVPs

    Just because you caught the exception, doesnt mean you have to throw it.  You can put a return in the catch block if you like as well.

  • Re: interesting return issue in catch block.

    11-19-2007, 3:55 PM
    • Member
      288 point Member
    • sahajMarg
    • Member since 05-03-2007, 6:59 PM
    • Posts 583

    thanks for your reply. Now in my example it is a string but lets say I have an Object named myObject.

    So should i then return a NULL object back? 

     

  • Re: interesting return issue in catch block.

    11-19-2007, 5:59 PM
    Answer
    • Star
      12,508 point Star
    • Freakyuno
    • Member since 01-20-2005, 4:57 PM
    • Midwest - United States
    • Posts 1,952
    • TrustedFriends-MVPs

    I guess I'm not sure why you'd return a generic object, in the world of complex types these days, but if you really need to work with a generic object, then yes - I'd probably return an empty object.  I dont think you can set an object to null.  Null is a value, and objects dont have values, objects are types in themselves, so you'll probably have to set it to nothing and return it, which seems strange to me actually, but in essence, you are returning nothing.

    Honestly, I usually ignore the warning about functions returning on all code paths, when it's because of a try catch block.  But I usually have incorporated some type of global event or custom error handling, so when I catch an exception, I throw a friendly error message.

  • Re: interesting return issue in catch block.

    11-20-2007, 9:35 AM
    • Member
      288 point Member
    • sahajMarg
    • Member since 05-03-2007, 6:59 PM
    • Posts 583

    In the following example If i don't throw an exception in the catch block, I get an compilation error. so I didn't get when you said you just ignore this warning, it is not a warning. I get compilation error on this. So i am forced to return either the object ( myAPP in this case) or throw an exception in the catch block. Thanks.

     

    public static ChannelApp   ParseDSXML(DataSet ds)
     {

    ChannelApp myAPP= new ChannelApp();

        try

        { 

                // Do something here 

            return  myAPP;

        } 

        catch

        {      
     

        } 

     

  • Re: interesting return issue in catch block.

    11-20-2007, 10:16 AM
    • Star
      12,508 point Star
    • Freakyuno
    • Member since 01-20-2005, 4:57 PM
    • Midwest - United States
    • Posts 1,952
    • TrustedFriends-MVPs

    You shouldnt get a compile time error just for not handling a catch. 

     Try

    catch ex as exception

     

  • Re: interesting return issue in catch block.

    11-20-2007, 10:52 AM
    • Member
      288 point Member
    • sahajMarg
    • Member since 05-03-2007, 6:59 PM
    • Posts 583

    This is what I get and it is a compilation error. 

    Error    3    'ReadParseXML.ParseDSXML(System.Data.DataSet)': not all code paths return a value    C:\Downloads\ChannelPartner\App_Code\ChannelApplication\ReadParseXML.cs    25    30    C:\Downloads\ChannelPartner\

  • Re: interesting return issue in catch block.

    11-20-2007, 8:17 PM
    Answer

    Hi:

      You need to return a value for all the code path. Try to add:

    return  myAPP;

      at the end of the method.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (8 items)