unreachable code?

Discussion in 'C++' started by Gernot Frisch, Apr 27, 2006.

  1. I get informed that parts of my code cannot be reached. Did I do
    something wrong?

    try
    {
    if (login(progstring) > 0)
    {
    return 1; // OK
    }
    }
    catch(int) // No hardlock
    {
    try
    {
    logout(NULL); // unreachable code
    }
    catch(int)
    {;}
    g_Version = MAX_VERSION; // unreachable code
    }




    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

    ________________________________________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    www.GLBasic.com
    Gernot Frisch, Apr 27, 2006
    #1
    1. Advertising

  2. Gernot Frisch wrote:
    > I get informed that parts of my code cannot be reached. Did I do
    > something wrong?
    >
    > try
    > {
    > if (login(progstring) > 0)
    > {
    > return 1; // OK
    > }
    > }
    > catch(int) // No hardlock
    > {
    > try
    > {
    > logout(NULL); // unreachable code
    > }
    > catch(int)
    > {;}
    > g_Version = MAX_VERSION; // unreachable code
    > }


    Could it be that in your compiler *somehow* exception handling is
    disabled? Could it be that 'login' is declared with "throw()" (as
    throwing nothing)? It's not your first day here. Post more code.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 27, 2006
    #2
    1. Advertising


  3. > Could it be that in your compiler *somehow* exception handling is
    > disabled? Could it be that 'login' is declared with "throw()" (as
    > throwing nothing)? It's not your first day here. Post more code.



    OK. I use VC++7.1. My compiler flags seem OK:
    /Od /FD /EHsc /RTC1 /MTd /GS /c /ZI /Gd /TP

    Here's the code (uncalled stuf removed)



    extern "C"
    {
    int login( const char *);
    int logout(const char *);
    void lib_pexit(int);
    SetFunctions(void*);
    }

    #define MAX_VERSION 2002
    int g_Version = MAX_VERSION;

    // get's called on error
    void lib_pexit( int ErrorCode )
    {
    throw (1); // C4297 unexpected exception raised
    }


    int CheckFeature(const char* progstring, double version)
    {
    SetFunctions((void*)&lib_pexit); // register "lib_pexit" function

    for (int nVersion = (int)version; nVersion<=tm.wYear+3; nVersion++)
    {
    g_Version = nVersion;
    try
    {
    if (login(progstring) > 0)
    {
    g_Version = MAX_VERSION;
    return 1; // OK
    }
    }
    catch(int) // no hardlock
    {
    try
    {
    logout(NULL);
    }
    catch(int)
    {;}
    g_Version = MAX_VERSION; // C4702 unreachable code
    }
    }
    g_Version = MAX_VERSION; // C4702 unreachable code
    return 0;
    }
    Gernot Frisch, Apr 27, 2006
    #3
  4. Gernot Frisch wrote:
    >>Could it be that in your compiler *somehow* exception handling is
    >>disabled? Could it be that 'login' is declared with "throw()" (as
    >>throwing nothing)? It's not your first day here. Post more code.

    >
    >
    >
    > OK. I use VC++7.1. My compiler flags seem OK:
    > /Od /FD /EHsc /RTC1 /MTd /GS /c /ZI /Gd /TP
    >
    > Here's the code (uncalled stuf removed)
    >
    >
    >
    > extern "C"
    > {
    > int login( const char *);
    > int logout(const char *);
    > void lib_pexit(int);
    > SetFunctions(void*);
    > }
    >
    > #define MAX_VERSION 2002
    > int g_Version = MAX_VERSION;
    >
    > // get's called on error
    > void lib_pexit( int ErrorCode )
    > {
    > throw (1); // C4297 unexpected exception raised
    > }
    >


    I would assume that "C" functions must not throw, so lib_pexit is not expected
    to throw and CheckFeature is not prepared to catch the exception raised by a
    "C"-function (->unreachable code)
    hth
    Klaus
    klaus hoffmann, Apr 27, 2006
    #4
  5. Gernot Frisch

    pillbug Guest

    >
    >>> Could it be that in your compiler *somehow* exception handling is
    >>> disabled? Could it be that 'login' is declared with "throw()" (as
    >>> throwing nothing)? It's not your first day here. Post more code.

    >>


    >> OK. I use VC++7.1. My compiler flags seem OK:
    >> /Od /FD /EHsc /RTC1 /MTd /GS /c /ZI /Gd /TP


    > I would assume that "C" functions must not throw, so lib_pexit is not
    > expected to throw and CheckFeature is not prepared to catch the
    > exception raised by a "C"-function (->unreachable code)
    >


    /EHc extern "C" defaults to nothrow.
    pillbug, Apr 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob

    Re: unreachable statements

    Jacob, Aug 16, 2004, in forum: Java
    Replies:
    2
    Views:
    380
    Joona I Palaste
    Aug 16, 2004
  2. Unreachable

    , Sep 6, 2003, in forum: Python
    Replies:
    1
    Views:
    391
    Bertel Lund Hansen
    Sep 6, 2003
  3. Unreachable

    , Sep 6, 2003, in forum: Python
    Replies:
    0
    Views:
    345
  4. Jack Bates
    Replies:
    2
    Views:
    237
    Gregory Ewing
    Apr 28, 2011
  5. bob smith

    unreachable code

    bob smith, Jul 19, 2012, in forum: Java
    Replies:
    5
    Views:
    268
    Roedy Green
    Jul 20, 2012
Loading...

Share This Page