Thursday, November 11, 2010

Why You Shouldn't Use Exception Specifications

I've significantly improved my coding style today...

I don't know if anyone else is guilty of this, but I have always used exception specifications religiously (see below if you are unsure as to what an exception specification is). I did this because of some very strange logic that now, looking back, seems very twisted.

My change of heart came about from reading a very well reasoned blog post. I thoroughly recommend that you read this as well if you like to use exception specifications.

Note
Example of an Exception Specification
double divide(int znumerator, int zdenominator) throw(std::invalid_argument) { if (zdenominator == 0) throw(std::invalid_argument("zdenominator cannot be 0")); return (double)znumerator / zdenominator; }

In the above code fragment, throw(std::invalid_argument) is the exception specification.

No comments:

Post a Comment