开发者

Catch statements are being completely ignored

I've run into a situation with some code I inherited... honestly, I believe the code is written correctly, but this error still seems to manifest.

I'll quickly note that 开发者_Python百科the code is cross-compiled from linux to LynxOS, I'm not sure if that can have anything to do with the error.

Basically, in one specific case:

try {
    std::vector<ClassA> x = SomeGeneratingFunction();

    //We get to here fine.  X may be empty/unpopulated though.
    if (x.size() < 1)
    {
        throw(MyException("It crashed."));
    }
}
catch (MyException e)
{
    //Handle it.
}
catch (...)
{
    //Handle it.
}

We throw given the vector is unpopulated, but for some reason the throw bypasses the catch clauses - both of them. It only seems to happen here - though we woudln't usually do it form an if statement scope, but that should be completely irrelevant since its still in the try scope.

PS: The code below is actually the contents of a function, and exceptions come out of the function when called even though they should all be handled by the catch blocks.

Any ideas how this is possible? And yes, this isn't the real code/exception classes, but the exception class is the simple example you'd google of overloading std::exception, and the SomeGeneratingFunction() does return a good vector, even if it is empty. I cannot provide the real code, but this is exceedingly close barring any little typos I may have made writitng it off the top of my head.


Since the catch (...) clause didn't catch the exception, my answer does not solve the OP's problem. But for others that found this question on SO, maybe my answer is useful, because it explains why the first catch failed.

I had a similar issue where my catch(const std::exception& ex) was just not working. It turned out to be a dumb problem in that I was switching between C# and C++ exceptions, and in C# you need to specify new when you throw your exception, while in C++ you normally don't (but you can, but in this case you are throwing a pointer and not a reference). I was accidentally doing

throw new std::runtime_error("foo");

so

catch(std::exception*  ex)

would have caught it but

catch(std::exception& ex)

doesn't. Of course, the solution is just remove the new statement, as that is not the traditional design pattern in C++.


Since you have a spare set of parentheses around the exception object in the throw statement, it looks like a function call. Is there any possibility that you've defined a function called throw? The parameter to the exception constructor keeps this from being a victim of the Most Vexing Parse, but that's a possibility if your actual code differs from your example.


catch (MyException e)

should read:

catch (const MyException &e)

I'm not sure why your throw looks like a function either, kind of bizarre.

Edit:

I've had problems with catches based on something similar, though I agree that it isn't sufficient in this toy case.

The compiler seems off to me, is Try defined as something funny? If it were empty it could make sense if your compiler ignored catch statements with no try.


If you have a function MyException in a smaller scope (possibly even by accident, Most Vexing Parse) then throw MyException("It crashed") will call that function and throw the return value.


Let me slash the problem... Don't use exceptions in C++ unless you want to crash definitely the program. I know exceptions may be quite useful, but they have very poor performances because it raises up an hardware exception which is caught by the operating system kernel. Using return values and error codes can be 500 times faster than throwing and catching exceptions.


I would just rewrite it without using try/catch. Doesn't seem appropriate to use try/catch here anyway. For all you know, exceptions may not work on your target platform.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜