Below is an excerpt from "Programming: Principles and Practice Using C++". I'm confused by the throw Bad_area() notation. The book tries to explain it, "Bad_area() means 'Make an object of type Bad_area'," continuing with that it then throws that type. This explanation is not congruent with the assignment notation, eg. int x=1 == int x(1); or Bad_area x;.
Example code (commented out the try-block):
class Bad_area {}; // a type specifically for reporting errors from area()
// calculate area of a rectangle
// throw a Bad_area exception in case of a bad argument
int area(int length, int width)
{
if (length<=0 || width<=0) throw Bad_area();
return length*width;
}
int main()
try {
// ...
}
catch (Bad_area) {
cout << "Oop! bad arguments to area()\n";
}
Bad_area()
is a explicit call to the default constructor of the classBad_area
.That is, what
throw Bad_area()
does is to return (throw) directly an anonimus instance of the classBad_area
.Is the same as in OO languages like Java or C#, when you return a instance directly. For example:
Note that that explicit calls to constructors are rarelly used in C++, because the lifetime of the instances is based on RAII.
There are only a few cases where a explicit call is a good idea, most of them are like your example, return statements. For example a
point_2d
class wich algebraic methods are easilly inlined:On the other hand, execpt that exceptional cases, direct calls to constructors must be avoided. Its common to see newebbies C++ code with a Java style. For example:
Or a correct C++ variable declaration, followed by a Java initialization:
Depending on the compiler, or if the optimizations are turned off (Everybody knows that noobs never enable optimizations...), this result in:
point_2d::operator=
Or, finally, the same but everything in the same line:
That is a call to the point_2d constructor followed by a call to the point_2d copy constructor to initialize the variable with the created temporal.
Note that performance in this case is not the point, because thats not the C++ stye/way to do things. Everybody who writes C++ code in that way, should go to buy a good C++ book. Effective C++ was redited with upcoming Java-like programmers in mind.