user788171
user788171

Reputation: 17553

C++: bug disappears when try/catch added to code

I have encountered a very strange error that maybe somebody can help me make sense of. I have a code that crashes quite consistently with vector out of range error. Another odd thing about this error is that when I toss in lots of cout statements throughout the code to try to pin down the location of the error, it stops crashing.

Anyways, as a further test, I put try/catch blocks around parts of the code, my implementation is something like:

try {
    // my code that is presumably causing problems
  }
  catch (out_of_range& oor) {
    cerr << "Out of Range error: " << oor.what() << endl;
  }

When I add in this code, the bug seems to go away. There are no more crashes, and I never see the cerr message in the catch.

Is there any possible way this can make sense?

Upvotes: 0

Views: 224

Answers (1)

djechlin
djechlin

Reputation: 60768

Step through old code in debugger and isolate error. Step through new code in a debugger and isolate non-error. I'm posting this as an "answer" and not a comment because it will give you the answer you are looking for in less time than it will take us to answer or you to read our answers.

Debugger in ten words: compile with -g, google "gdb cheatsheet", it's very simple.

Upvotes: 1

Related Questions