Catch statements are completely ignored

I came across a situation with some inherited code ... to be honest, I believe the code is written correctly, but this error seems to still be showing up.

I will quickly note that the code cross-compiles from Linux to LynxOS, I'm not sure if this might somehow be due to an 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 that the vector is not populated, but for some reason the throw circumvents the catch clauses - both of them. It just seems to be happening here - although we usually do not do this by forming the scope of the if statement, but this should be completely irrelevant, since it is still in the scope of the try.

PS: the code below is actually the contents of the function, and exceptions are thrown from the function when called, although they should all be handled by catch blocks.

Any ideas how this is possible? And yes, these are not real code / exception classes, but the exception class is a simple example by which Google overloads std :: exception, and SomeGeneratingFunction () returns a good vector, even if it is empty. I cannot provide real code, but it is very close, with the exception of any typos that I might have written on the top of my head.

+8
source share
6 answers

I would just rewrite it without using try / catch. It seems impractical to use try / catch here anyway. As far as you know, exceptions may not work on your target platform.

-one
source

Since the catch (...) clause did not detect an exception, my answer does not solve the OP problem. But for others who have found this question on SO, maybe my answer is useful because it explains why the first catch failed.

I had a similar problem when my catch(const std::exception& ex) just didn't work. This turned out to be a stupid problem in that I switched between C # and C ++ exceptions, and in C # you need to specify new when you throw your exception, and in C ++ usually not (but you can, but in this case you are throwing a pointer, not a link). I accidentally did

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

So

 catch(std::exception* ex) 

would catch him but

 catch(std::exception& ex) 

not. Of course, the solution simply removes the new operator, since this is not a traditional C ++ design pattern.

+11
source

Since you have a spare set of parentheses around the exception object in the throw statement, it looks like a function call. Is it likely that you have defined a function called throw ? The exception constructor parameter may not be a victim of Most Vexing Parse , but it is an option if your actual code is different from your example.

+1
source
 catch (MyException e) 

should read:

 catch (const MyException &e) 

I'm not sure why your cast looks like a feature, as if bizarre.

Edit:

I had problems with catches based on something similar, although I agree that this is not enough.

The compiler seems unavailable to me, Try is defined as something funny? If it were empty, it might make sense if your compiler ignored the catch statements without trying.

0
source

If you have a MyException function in a smaller area (perhaps even by accident, most Vexing Parse), then throw MyException("It crashed") will call this function and reset the return value.

0
source

Let me tackle the problem ... Do not use exceptions in C ++ unless you want a specific program error. I know that exceptions can be very useful, but they have very low rates, because they raise a hardware exception that gets into the kernel of the operating system. Using return values ​​and error codes can be 500 times faster than throwing and catching exceptions.

-3
source

All Articles