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

This is a discussion on Is there a catch to return in a try/catch box? within the C++ Programming forums, part of the General Programming Boards category; Code: try{ ... return true; }catch(...){ ... } Is there a catch?...

  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,340
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What do you mean?
    Are you having a problem?

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

  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,893
    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
    Jan 2005
    Posts
    7,340
    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).

  9. #9
    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;

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    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 05:39 PM.

  11. #11
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,940
    Yes, I believe CornedBee meant RAII. The simple examples would be the use of containers that manage their own memory, and/or smart pointers.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,790
    I don't find it bad at all... If, say, you have a function that return a boolean of success or failure, you could return true within your try and false within your catch.
    When it's somewhat more complicated, like returning a error, or exception class, I typically have a global pointer at the start of the function, init it to NULL and if there's an error, create an object with it and at the end, after try/catch, return that var.
    Otherwise, you can also return within your try if you're doing a catch/rethrow approach. Basically, if some function within your function/class fails, then the parent function catches that exception, cleans up, and rethrows it. In that case, there's no need for a return since it throws and thus I usually place a return in the try block.

    I don't know... I suppose it's up to each and everyone?

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> if some function within your function/class fails, then the parent function catches that exception, cleans up, and rethrows it.

    Hopefully your code is structured with RAII so that the cleanup is automatic and you don't even need a try/catch block in that function at all.

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21