Weird exception issue.

Hi All,
I have a class constructor 'DBInterfaceTests()' which calls the member function of an object of another class. That call makes a hard-coded throw of type Debugging::Assertion, and the return type of Debugging::Assertion::what() is 'const char*'. In the following code compiled with gcc on Linux, only the output from the second catch block is seen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using std::cout; using std::endl;
using std::cerr;

#include "Debugging.hpp"
#include "DBInterfaceTests_Mirror.hpp"

int main() {
  try {
    DBInterfaceTests dbi;
  }
  catch (Debugging::Assertion& ex) {
    cout << "(cout) Debugging::Assertion: " << ex.what() << endl;
  }

  try {
    DBInterfaceTests dbi;
  }
  catch (Debugging::Assertion& ex) {
    cerr << "(cerr) Debugging::Assertion: " << ex.what() << endl;
  }

    return 0;
}


If I make the call to the member function directly, instead of through the constructor of 'DBInterfaceTests', all is well.

There is no throw specification on either the constructor or the member function being called.

Before digging into the gory details, can anyone think why outputting to cerr would be different from outputting to cout here?

Last edited on
Is cout redirected?

What does this do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using std;

#include "Debugging.hpp"
#include "DBInterfaceTests_Mirror.hpp"

int main() {
  try {
    DBInterfaceTests dbi;
  }
  catch (Debugging::Assertion& ex) {
    cout << "(cout) Debugging::Assertion: " << ex.what() << endl;
    cerr << "(cerr) Debugging::Assertion: " << ex.what() << endl;
  }
    return 0;
}
cout isn't redirected intentionally, though it behaves as though it's redirected to /dev/null after the call to the constructor.

The output from this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using std;

#include "Debugging.hpp"
#include "DBInterfaceTests_Mirror.hpp"

int main() {
    cout << "Starting..." << endl;
  try {
    DBInterfaceTests dbi;
  }
  catch (Debugging::Assertion& ex) {
    cout << "(cout) Debugging::Assertion: " << ex.what() << endl;
    cerr << "(cerr) Debugging::Assertion: " << ex.what() << endl;
  }

    cout << "Finishing..." << endl;

    return 0;
}


is:
1
2
Starting...
(cerr) Debugging::Assertion: Assertion Message


Can cout be redirected from code?
Can cout be redirected from code?
Yes: http://www.cplusplus.com/reference/iostream/ios/rdbuf/
What's in DBInterfaceTests constructor?
Last edited on
I found the source of the problem. A stream output of type 'char *' was shutting cout down somehow. It appears that whatever sqlite3 returns for a NULL value in a call to 'sqlite3_get_table()' was making cout output stop. I'm not sure if it's because it's the right escape sequence for the terminal to do this, or because the value confused the stream too much.
Last edited on
It turns out that sqlite3 returns a NULL pointer (in a char* array) for a NULL value. The following code duplicates the issue:
1
2
3
4
5
6
7
int main() {
    cout << "Starting..." << endl;
    cout << static_cast<char*>(NULL) << endl;
    cout << "Finishing." << endl;

    return 0;
}


with the output being:
 
Starting...

and the program's return value being 0.

This wouldn't bother me much, but I expect an error of some sort rather than the behavior it's producing. Can anyone explain why it is doing this?
Maybe the empty string makes cout fail, resetting its state to good should make cout work again:
1
2
3
4
cout << "Starting..." << endl;
cout << static_cast<char*>(NULL) << endl;
cout.clear();
cout << "Finishing." << endl;

Thanks for everyone's help, that did it.
Topic archived. No new replies allowed.