Question

I get this terminating with uncaught exception even though I thought I had caught the exception. Here is some example code

#include <iostream>
#include <stdexcept>

void throwing(int x)
{
  if(x) throw std::runtime_error("x non-null");
}

int main()
{
  try {
    throwing(1);
  } catch(std::runtime_error const&ex) {
    std::clog << "error: \"" << ex.what() << '\"' << std::endl;
    std::terminate();
  }
  return 0;
}

which produces (after reporting error: "x non-null") the said message (using clang++ with std=c++11). So, when exactly has the exception been caught and hence deemed uncaught in the sense of terminate() not reporting it (again)? Or, equivalently (or not?): how can I catch an exception, report its what(), and terminate() without getting this blurb?

(I could refrain from reporting the what() and just terminate(), but I want to report the what() in my own way.)

Was it helpful?

Solution

Since noone provided an answer to this, and it seems like my comment was either not clear enough or overlooked, I'll post a full answer:

Or, equivalently (or not?): how can I catch an exception, report its what(), and terminate() without getting this blurb?

If you read carefully thru the reference, you'll notice std::terminate() by default calls abort(), which causes the program to terminate returning a platform-dependent unsuccessful termination error code to the host environment.

Now, answering the quoted question: either use exit(int status) (which is a better solution, since you handled the exception) instead of terminate, or change the terminate handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top