C++ Exception Problem

This is going to be a very technical post.

Gory details forthcoming, you’ve been warned.

I think I have a problem with some C++ code, and I’ve narrowed it down as much as I can.  I am using the standard exception, std::overflow_error (though that probably doesn’t matter).  I’m compiling with GCC 4.9.1 (this, I think, does matter).

So here’s the summary, with a fairly complete set of exception-handling clauses, when I throw the overflow_error exception from an activity by itself, it works; but if I have even a fairly simple string declaration and assignment, then all the exception handling gets skipped.

Details, Details

Let’s start with a “working” example; here’s the main body:


#define CHAR char
#define ATOL atol
#define MAIN main

Never mind what I was doing here


int MAIN(int argc, CHAR* argv[])
{
  SixFactor t(26757, -1);
  cout << "t = " << static_cast<std::string>(t) << endl;
  try {
    SixFactor e(0,0);
    e = t*t;
    cout << "got here - no exception" << endl;
  } catch (overflow_error &oe) {
    cout << "caught overflow exception: " << oe.what() << endl;
  } catch (...) {
    cout << "caught an unknown exception" << endl;
  }
  return 0;
}

Strange class name?  It’s pulled from the context of the program where I noticed this.

So what’s in the constructor(s), and the operator* overload function?


class SixFactor
{
  public:
    SixFactor(const ulong m, const int o): _mult(m), _offs(o) {};

and later

SixFactor operator*(const SixFactor &othr) throw(overflow_error) {
  // std::string err_msg = "SixFactor mult. overflow of (";
  // err_msg += static_cast(*this) + ") * (" +
  // static_cast(othr) + ")";
  /// cerr << "throwing an exception: " << err_msg << endl;
  // throw std::overflow_error(err_msg.c_str());
  ////////////////////////////////////////
  // If I uncomment even one of the preceding lines (the string decl. line)
  // then the exception below is *not* caught, but the program terminates
  // abnormally. As it stands w/ the exception only, it "works"

  throw overflow_error("this works");
}

Note that right now, the only un-commented code in this function is the throw statement.

What happens:

Compiling

g++  -g -std=c++11 -Wall -Wextra -v -Weffc++ -Wold-style-cast  -c
      -o test_exception2.o test_exception2.cpp

Running

./test_exception2

Output:

t = 6*26757 - 1
caught overflow exception: this works

When it breaks

Let’s change the code

SixFactor operator*(const SixFactor &othr) throw(overflow_error) {
  std::string err_msg = "SixFactor mult. overflow of (";
  // err_msg += static_cast(*this) + ") * (" +
  // static_cast(othr) + ")";
  /// cerr << "throwing an exception: " << err_msg << endl;
  // throw std::overflow_error(err_msg.c_str());
  ////////////////////////////////////////
  // If I uncomment even one of the preceding lines (the string decl. line)
  // then the exception below is *not* caught, but the program terminates
  // abnormally. As it stands w/ the exception only, it "works"

  throw overflow_error("this works");
}

Note: all I did is add a declaration and assignment (never mind the string doesn’t mean anything).  It shouldn’t change the operation at all.  But it does.

./test_exception2
t = 6*26757 - 1

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
make: *** [test_exc2] Error 255

And Windoze puts up a dialog box:
err_dlg_box

 

So what’s going on here?

That’s my question.  I’m at the point of starting to examine assembly code; right now I don’t know, but I’m putting this out to the “world.”  In a sense, I’m crowd-sourcing for an answer, but I think my crowd might be too small.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: