An exception means “something went wrong that I can’t fix”.
Imagine that you have an employee that is told to move all the files from their old cabinets and put them into new cabinets, and to organize them a new way.
Said employee trips down the stairs with a cabinet and dies.
Now you have a mess: files everywhere, with some organized in the new system in the new cabinets, some still in the old cabinets, and a whole bunch of unorganized files lying on the floor.
When the janitor comes by later and discovers the dead employee (and file mess) he reports it to the boss, saying “we’re toast”. That’s an exception.
The point is that the exception was caught (the boss was told about it) and can decide to clean up the mess. (“Alright, go pick up all the files and stick them back in the original cabinet. And... call the coroner.”)
Having caught the exception the program can perform damage control and continue running. It just didn’t finish its task.
If the exception were not caught, the program would either crash and burn or produce incorrect output.
───────────────
I have not read the book you reference. The example code posted has several errors.
(1) You cannot declare a function in a catch block.
[1]
(2) Nothing in that code throws an exception to be caught.
[2]
(3) Use RAII to clean up.
[3]
The point is that whenever you are going to do something that may fail catastrophically, but from which failure does not necessitate termination, wrap it in a try..catch.
1 2 3 4 5 6 7 8 9 10 11
|
try
{
do_something_that_may_fail_horribly();
std::cout << "success!\n";
}
catch (...)
{
std::cout << "fooey! things did not work out.\n";
}
std::cout << "let's do more stuff now.\n";
|
I do not know why the cleanup is trying to read more data from the stream.
Unfortunately, with just that snippet, there is not enough context to understand what the author is trying to do.
Disclaimer: I am not a fan of Malik. I think his coding skillz are trash, yo. IMNSHO, the design behind his tokenizer smells. Since before C++ existed there have been cleaner ways of doing a lookahead tokenization, which he seems to be attempting but failing fairly spectacularly.
What RAII means is that if you have opened/accessed/whatever a resource that needs to be cleaned up when you are done with it, you should have an object whose lifetime controls that resource. File streams do this, for example. You can open the file, but you do not need to close it explicitly — let it do that automagically when the file object is destroyed at the end of its scope:
1 2 3 4 5 6 7 8 9
|
std::vector <std::string>
read_lines( const std::string& filename )
{
std::vector <std::string> lines;
std::ifstream f( filename ); // open the file, if possible
std::string s;
while (getline( f, s )) lines.emplace_back( s ); // get all available lines (if file is open)
return lines; // all done!
} // end of scope. s and f and lines are all destroyed.
|
In that example, you notice that we never bother to close the file. But it happens anyway when
f
is destroyed at the end of the function. Convenient!
If I were to write my own file stream class I could do it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
struct ifstream
{
private:
FILE* fp; // Here is our external resource token that we MUST manage
public:
ifstream( const std::string& filename ): fp(nullptr)
{
// Here we get access to the external resource
fp = fopen( filename.c_str(), "r" );
}
~ifstream()
{
// And here we release the external resource (automatically for the user when )
if (fp) fclose( fp );
}
// ...and lots of other stuff, like operator>> etc.
}
|
See how we use the object’s lifetime (in particular, its destructor) to clean up the external file resource for us? That’s RAII: acquire the resource in the constructor and dispose of it in the destructor.
───────────────
[1] You can pretend to with lambdas, but ignore that for now, as it does not apply to your situation.
[2] Unless you have turned on the evil “throw an exception on any error” iostream flag. (Which I doubt the book has done.)
[3] Yes, a horrible acronym.
Hope this helps.