2

I have CLion installed with presumably default configuration. I think something is wrong with it, because I can't see exceptions. For example, this code:

int main(){ throw 5; }

Prints only Process finished with exit code 0

  • Why doesn't it print the exception?
  • Why does it print 0 instead of 1?

For comparison:

int main(){try { throw 5; } catch(int x) { std::cout << x << '\n'; }}

This prints 5, so it looks like code is correctly run and the exception is correctly thrown. It's just hidden by CLion somehow.

Edit: This is not a duplicate of "not seeing any console output". I made extremely clear in my question that I was indeed seeing console output for prints. My issue concerns exceptions specifically, not console output generally.

Atte Juvonen
  • 4,922
  • 7
  • 46
  • 89
  • 3
    This is many things, but not a duplicate of a question asking how to get console output in CLion - the question is if it is possible to print uncaught exceptions (that end up calling `std::terminate`), and why it shows an exit code of 0 after `std::terminate` (when the default termination handler of `std::abort` should return an exit code indicating unsuccessful termination). The other duplicate is about exceeding array bounds, not about C++ exceptions. – hlt Oct 03 '18 at 09:17

1 Answers1

1

In your first piece of code you have not caught the exception, so it is handled by the default handler. You therefore have no control over the return code from the executable. Operation is as expected.

If you would like CLion to display the exception you can configure it to do so. Note that this will only apply when CLion is debugging your executable, outside of CLion your executable will continue to behave as you have already seen.

  • Go to run then view breakpoints.
  • Go to exception breakpoints and when any is thrown.
  • To display the stack trace when the exception is thrown check stack trace
  • If you would like your program to stop for your intervention, check enabled and when thrown.
  • Make sure to use Run/Debug and not Run/Run to launch your program.
c z
  • 7,726
  • 3
  • 46
  • 59