CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding try/catch

    Here is the code(psedo code),
    Code:
    bool foo()
    {
         try{
                if thing one is not done
                    return false;
     
                if thing two is not done
                    return false;
                
         }
         catch(...)
         {
               do something...
         }
    
         return true;
    }
    My question is that within catch clause, should I call return false at the end? Thanks.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: A question regarding try/catch

    It only depends on what the return value of this function means.
    If the case of exception means false then return false, it it means true then return true.
    If it depends upon the exception code/error number/or something similar - then it is up to you to choose what to return: true or false...
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding try/catch

    Quote Originally Posted by VictorN View Post
    It only depends on what the return value of this function means.
    If the case of exception means false then return false, it it means true then return true.
    If it depends upon the exception code/error number/or something similar - then it is up to you to choose what to return: true or false...
    The problem is there is no way to decide whether it should return true or false when an exception is thrown. For example, when we call the statement "if thing one is not done" , it may throw an exception. So in this case, should I return true or false in the catch clause? This is really confusing...

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A question regarding try/catch

    More typically you'd throw in the function and put the try/catch in the function that calls it. That way you'd know if it returned legitimately or threw the exception.

    Code:
    void bar()
    {
        try
        {
              if(foo())
                    ...
        }
        catch(...)
        {
            ...
        }
    }
    If you need the catch in foo, typically returning true would indicate foo succeeded, and false would indicated it didn't.
    Last edited by GCDEF; January 23rd, 2013 at 09:11 PM.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: A question regarding try/catch

    Quote Originally Posted by LarryChen View Post
    Here is the code(psedo code),
    Code:
    bool foo()
    {
         try{
                if thing one is not done
                    return false;
     
                if thing two is not done
                    return false;
                
         }
         catch(...)
         {
               do something...
         }
    
         return true;
    }
    My question is that within catch clause, should I call return false at the end? Thanks.
    You could try the "update the return value as you go" scheme:

    Code:
    ReturnType foo()
    {
         ReturnType return_val = a;
         try{
                return_val = b;
                if (thing_one_is_not_done)
                {
                    return_val = c;
                    return return_val;
                }
     
                return_val = d
                if (thing_two_is_not_done)
                {
                    return_val = e;
                    return return_val;
                }
                
                return_val = f
         }
         catch(...)
         {}
    
         return return_val;
    }
    You can update this scheme: instead of doing "if-return; if-return", you can go for the "if{if{if{ifi{}}}}" scheme. You can replace the intermediate "return return_val" with "goto end;".

    Regardless of what you are doing, the scheme of "update your return value as you go" is pretty to use. Its not the most efficient, but more often than not, it makes coding really easy.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured