Strange C ++ exception handler issue

I have used standard C ++ exception handling techniques. These are try {} and catch {} block. In my code, func1 () throws an exception and func2 looks like this:

bool func2()
{
    try{
       func1();
    }

    catch(myException& e)
    {
       cerr << "error!" << endl;
       return false;
    }
    return true;
}

      

But when I run my code, a strange thing happens. I never got to the code for throwing an exception, but I always got to the return false line in the catch block (but the line was cerr <<

never reached). The function then continues to return true. I do not know the reason. Can anyone help me figure out the problem? Thank you very much!

0


source to share


8 answers


If you use any optimization flags in your compiler (i.e. not debug mode), you cannot trust the debugger to show the correct execution lines. You have made conflicting statements - the statement return false is executed, but the function returns true. This is the only explanation I can think of.



+7


source


As @Mark Ransom said, you probably have optimization enabled. To help you understand what's going on, this is the C ++ equivalent of the code the compiler is likely generating:

bool func2()
{
    bool ret;

    try{
       func1();
       ret = true;
    }

    catch(myException& e)
    {
       cerr << "error!" << endl;
       ret = false;
    }

    return ret;
}

      



However, when you are on a line return ret;

on the generated code, there are two lines in the source code that you really could be on: return false;

and return true;

. The debugger has no idea what is correct and so it displays the first one (in fact, it is the compiler when creating the tables that the debugger that makes the selection will use).

It will be much more obvious if you tell the compiler to generate an assembly file and look at it. Assuming it's x86 where the return value is at %eax

, both branches will set %eax

to 1 or 0 and then jump or leak into the generic epilogue code (the function where it actually returns from the function).

+3


source


Open the application in WinDbg (or gdb) and enable the first exceptions (in WinDbg via sxe eh).

0


source


How do you know it returned false? If func2 did not execute cerr and return true, it looks like no exception was caught and return false was not executed.

If you are using the debugger, it will sometimes show that you are on the closing parenthesis for the catch block when no exception is thrown, which can be confusing.

0


source


If you use the return true statement, does a different type of exception occur? Try adding catch (...).

0


source


I noticed that sometimes cerr can act werid, have you tried replacing cerr with cout? Also try putting two cout in an exception block.

0


source


If no output is generated and the RETURNING function is true.

Then the exception does not exclude func1 ().
As a result, the function returns true.

But what you may be experiencing is the inability of debuggers to match machine instructions to the C ++ source code after the code has been optimized (as the optimizer might tweak the code).

I suggest you try something like below to make sure you report all exceptions.

Try the following:

bool func2()
{
    try
    {
         func1();
    }
    catch(MyException const& e)
    {
         std::cerr << "Error!: Cought My Exception\n";
         return false;
    }
    catch(...)  // Catch any other exceptions
    {
        std::cerr << "Error!: Unknown Exception detected\n";
        /*
         * To keep the same functionality of your original code
         * Rethrow the unknown exception.
         */
        throw;
    }
    return true;
}

      

0


source


Use debug mode, not optimized mode. Also, make sure you don't write any dangling pointers.

-1


source







All Articles