Thread: Is there a catch to return in a try/catch box?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Is there a catch to return in a try/catch box?

    Code:
    try{
    ...
    return true;
    }catch(...){
    ...
    }
    Is there a catch?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you be more specific with your question?

    The code inside the catch block will only be run if an exception is thrown by the code in the try block. If not, then the the return will execute. If no exception is thrown, the catch block code will never be run regardless of whether the return statement is there or not.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Is that to say, the code is as safe as:
    Code:
    try{
    ...
    }catch(...){
    ...
    }
    return true;

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What do you mean?
    Are you having a problem?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by cpjust View Post
    What do you mean?
    Are you having a problem?
    I am not having a problem. I just want to err on the caution side.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Since the code does nothing, it has the same safety in both cases.

    The best way, however, is to not have catch-all clauses at all. Use RTTI for resource management, and the situations where you need a catch-all will be very rare.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by CornedBee View Post
    Since the code does nothing, it has the same safety in both cases.

    The best way, however, is to not have catch-all clauses at all. Use RTTI for resource management, and the situations where you need a catch-all will be very rare.
    Thank you, I have my own exception class. Here I used catch(...) just as an example.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Quote Originally Posted by CornedBee View Post
    Use RTTI for resource management, and the situations where you need a catch-all will be very rare.
    Did you mean RAII? If not, could you please give a simple example how to "manage resources with RTTI"? Thank you.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    These two pieces of code are different:
    Code:
    try{
    ...
    return true;
    }catch(...){
    ...
    }
    Code:
    try{
    ...
    }catch(...){
    ...
    }
    return true;
    The first does not return anything from the function if an exception is caught (unless there is a return statement somewhere else in the code not shown). The second one will always return true whether an exception is thrown or not (unless there is a return statement somewhere else in the code not shown).

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    There shouldn't be any exceptions thrown in a return statement unless you're actually executing something in the return, like this:

    Code:
    return SomeFunc();
    
    or
    
    return new Obj;

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    The following 2 pieces of code are the same, aren't they?

    Code:
    try{
    ...
    return true;
    }catch(...){
    ...
    return false;
    }
    Code:
    try{
    ...
    }catch(...){
    ...
    return false;
    }
    return true;

    Quote Originally Posted by Daved View Post
    These two pieces of code are different:
    Code:
    try{
    ...
    return true;
    }catch(...){
    ...
    }
    Code:
    try{
    ...
    }catch(...){
    ...
    }
    return true;
    The first does not return anything from the function if an exception is caught (unless there is a return statement somewhere else in the code not shown). The second one will always return true whether an exception is thrown or not (unless there is a return statement somewhere else in the code not shown).

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by meili100 View Post
    The following 2 pieces of code are the same, aren't they?

    Code:
    try{
    ...
    return true;
    }catch(...){
    ...
    return false;
    }
    Code:
    try{
    ...
    }catch(...){
    ...
    return false;
    }
    return true;
    Yes, they should act the same.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think he is catching an exception and then returning, not trying to catch an exception on the return.

    I do not recommend returning inside of a try block but inside of a catch block you may have to.

    Code:
    try 
    {
       pSomeObject->DoSomethingThatMayThrow();
    }
    catch (MyException &me)
    {
      ...
      return;
    }
    I don't like returning mid function but sometimes it is necessary since the code beyond the try/catch block obviously assumes that the function executed succesfully. If not and you continue then the whole point of catching the exception to avoid crashing is null and void.

    The only other alternative is to set a boolean in the catch block and then execute an if beyond the try/catch that will exit if true and continue if false. To me though that is a lot of work just to maintain one entry one exit and may actually make the code harder to read.
    Last edited by VirtualAce; 11-20-2007 at 06:39 PM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, I believe CornedBee meant RAII. The simple examples would be the use of containers that manage their own memory, and/or smart pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Oops, typo. Yes, I meant RAII.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM