A question regarding try/catch
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: A question regarding try/catch

  1. #1
    Join Date
    Jul 2005
    Posts
    894

    A question regarding try/catch

    Suppose someone else writes a function foo like this,

    Code:
    void foo()
    {
           try{
                  do something...
           }
           catch(CSomeException* e)
           {
                  I catch an exception...
           }
    }
    Assume I can't change the code above. If I call the function foo, how can I catch the exception CSomeException? If I do something like this,
    Code:
          try{
               foo();
          }
          catch(CSomeException* e)
          {
               I also want to catch an exception...
           }
    I am never going to catch the exception since the function foo has already taken care of it. So my question is that in this case how am I able to catch exception CSomeException? Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,249

    Re: A question regarding try/catch

    If foo does not re-raise the exception, then there is no way to do what you want given the scenario. Of course, you might be able to change the scenario, e.g., don't call foo.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,423

    Re: A question regarding try/catch

    Quote Originally Posted by LarryChen View Post
    Assume I can't change the code above. If I call the function foo, how can I catch the exception CSomeException?
    The foo() function, according to you, is a black-box -- whatever it does, it does, and you can't change it. All the internals inside of foo() means nothing to you, as long as foo() does its job.

    Well, if you can't change the code, and no one documented to you that foo() throws an exception to the caller, then why write the code to call foo() with try/catch in the first place? You shouldn't write try/catch blocks unless someone has documented that the function throws an exception back to the caller. The foo() function does not throw an exception back to the caller, so no need to introduce try/catch around a call to foo(). Simple.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jul 2005
    Posts
    894

    Re: A question regarding try/catch

    The function foo is designed like that turns out to be bad, right? So my understanding is that the function foo either re-throws that exception within the catch clause or doesn't catch that particular exception at all. Correct me if I am wrong. Thanks.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

    Re: A question regarding try/catch

    Quote Originally Posted by LarryChen View Post
    The function foo is designed like that turns out to be bad, right? So my understanding is that the function foo either re-throws that exception within the catch clause or doesn't catch that particular exception at all. Correct me if I am wrong. Thanks.
    You didn't show enough code to know what it does, but it appears it will catch anything derived from CSomeException and handle it itself.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,249

    Re: A question regarding try/catch

    Quote Originally Posted by LarryChen
    The function foo is designed like that turns out to be bad, right?
    As GCDEF noted, there is not enough information. The pattern that you showed us in this reduced version of foo is so generic that to call it bad means to call every single use of try/catch bad design.

    Quote Originally Posted by LarryChen
    So my understanding is that the function foo either re-throws that exception within the catch clause or doesn't catch that particular exception at all. Correct me if I am wrong.
    No, you have to refer to the implementation and/or documentation of foo (or whatever its real name is) to correct yourself if you are wrong. What foo does depends on its implementation, and we really don't know what is its implementation since you did not show it to us.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center