Thread: warning C4702: unreachable code

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    warning C4702: unreachable code

    Why would VC++ tell me "warning C4702: unreachable code" for code that is reachable?
    Does it search through the entire program to see every possible way that the function can be called and then determine that it's impossible to reach, or does it just search through each function separately?

    I'm getting that warning on a lot of catch (...) blocks like:
    Code:
    catch (...)
    {
       return 0;  // warning C4702: unreachable code
    }
    Unless it looks through every function called in the try block to see if any of them throw exceptions, how can it possibly determine that the return is unreachable?
    And why would it only start complaining about that now, when I already compiled those same functions in the same files cleanly in different projects?
    I swear my compiler must be on drugs or something.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Can't tell from that snippet...

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    http://msdn.microsoft.com/en-us/libr...0e(VS.80).aspx

    Do you have code in the try block that can throw?

    gg

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    Can't tell from that snippet...
    Well I can't paste my company's actualy source code, but in general, any time you call functions in a try block, how can you possibly say that catch (...) is unreachable? The functions don't use exception specifiers either, not that they really guarentee much anyways.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Codeplug View Post
    http://msdn.microsoft.com/en-us/libr...0e(VS.80).aspx

    Do you have code in the try block that can throw?

    gg
    Hmm... That might be it. I'll have to check to see if all the functions are extern "C" functions.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, VC++ does do cross-function, even cross-module optimization, and the unreachable code warning typically piggy-backs on the optimizer's flow analysis. So yeah, the compiler may well be capable of deciding that your code is unreachable.

    Why do you have catch-alls all over the place anyway. Looks like misdesign to me.
    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
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    Why do you have catch-alls all over the place anyway. Looks like misdesign to me.
    I didn't write this junk. I just have to fix it.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I also see a lot of try blocks around C functions, which I'm guessing is to catch exceptions from de-referencing NULL pointers.
    VC++ 2005 doesn't catch something like:
    Code:
    strcpy( NULL, "Hello world" );
    but VC++ 6.0 does catch it in the catch (...) block.
    The funny thing is, in VC++ 6.0 it gives you a C4702 in the catch (...) block, even though it does actually catch it on that broken compiler.

    You just gotta love that undefined behavior.
    Last edited by cpjust; 05-05-2008 at 03:22 PM.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I believe the behavior would be defined by which "/EH" compiler option used.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using malloc
    By ulillillia in forum C Programming
    Replies: 34
    Last Post: 02-20-2008, 06:41 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Confusion : Warning long code
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 04-08-2003, 08:07 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM