Where does the text go in the excluded exception?

Possible duplicate:
How do I print a catch exception message?

Sorry if this is basic or duplicated - at first I did a few searches, but did not find anything that could answer this.

If I do something basic:

throw exception("This thing didn't work");

Where can I see it? The line is not displayed in the output console, stack trace, or any of the .log files associated with the project. It makes me wonder why I even put a line there if it is not visible anywhere. I can, of course, use the stack trace to see where it exploded, but this view defeats the goal of having exceptions in the first place.

In Java, when I give it a line, I will see this line in the output somewhere. I'm just wondering if it is possible to reproduce this behavior in C ++.

+5
source share
2 answers

You must catch the exception to see the string. It may be useful to have an exception handler in the latter case as follows:

int main(int argc, char **argv)
{
    try {
        ...
    }
    catch(const std::exception& e) {
        std::cout << "Uncaught exception: " << e.what() << std::endl;
    }
}

In a GUI application, you can have a catch statement in the loop of the main event and display an error message in the message box.

In an event-driven application without a GUI, you can also have a catch statement in the main event loop and write the error message to the log file.

These methods will allow you to see all the exceptions. Of course, most exceptions should be caught before they reach your last-resort exception handler.

+1
source

std::exception()has a member function called what(). Cm:

http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-3.3/classstd_1_1exception.html

, std::logic_error, , .

+5

All Articles