What does throw; (with no exception object after the throw keyword) mean? Where would
I employ it?
A: You should see code that looks something like this:
class MyException {
public:
...
void addInfo(const std::string& info);
...
};
void f()
{
try {
...
}
catch (MyException& e) { e.addInfo("f() failed"); throw;
}
}
In this instance, the statement throws; refer to "re-throw the current exception." At this time, a function caught exception (through non-const reference), modified the exception (through adding up information to it), and re-threw the exception then. This idiom can be used to implement a simple form of stack- trace, adding suitable catch clauses in the important functions of your program.
Another re-throwing idiom is "exception dispatcher":
void handleException()
{
try {
throw;
}
catch (MyException& e) {
...code to handle MyException...
}
catch (YourException& e) {
...code to handle YourException...
}
}
void f()
{
try {
...something that might throw...
}
catch (...) {
handleException();
}
}
This idiom let a single function (handleException()) to be re-used to handle exceptions in number of other functions.