Ya know, I would love to, but Stroustrup does a FAR better job than I ever could in chapter 5, and I'll just give you half advice. If I could summarize it, I would say:
Basically what you're doing is setting up simplified error handling that will give you a better message than the garbled nonsense you'll usually get when you encounter run-time errors. The try aspect is just "defining" scope to look for them (I guess you would say?) and catch is just where errors are "caught" to give you a meaningful message to output. For example:
1 2 3 4 5 6 7 8 9 10 11
|
catch(exception& e)
{
cerr << e.what() << endl;
exit(1);
}
catch (...)
{
cerr << "Unknown exception...\n";
exit(1);
}
|
The first function is what your error("MESSAGE") goes to if you supply it. Your message should be something that identifies where the problem was found and/or what it was, and it's passed along. The second function is, as the output suggests, when an exception was caught, but it's not well known.
1 2 3 4 5 6 7
|
struct Month{
int m;
Month(int month):m{month}
{
if(m < 1 || m > 12) error("Month must be between 1 and 12");
}
}
|
To be honest, I don't even like telling you that much because I don't know for certain the specifics, but that's what I know from using it, and I don't want to lead you down the wrong path. The other problem is that Stroustrup personalized it heavily for the text, so (as far as I am on chapter 19) I can't say for certain if he breaks it down further. Sorry :(
Also, you may find it useful (or possibly not), but I like to include __func__ (those are 2 '_' characters) to include the name of the function the error was found inside.