Exception specifications

I recently learned that the C++ draft standard requires that an implementation not perform any static checking on a function’s exception specification (aside from inherited virtual functions and function pointers). This makes the following legal (copied from the standard, sec 15.4 par 10):

extern void f() throw(X, Y);void g() throw(X){    f();  // OK}
Question 1: Why would they do such a thing?

Question 2: Are lint-like tools available that can give warnings about these situations?

It seems to me that without any static checking, the throw specifer is all but useless for a typical project.

Yes, you heard correctly. Static checking of exception specifications can bevery tedious and time-consuming for the compiler. Consider the following:

struct X {};struct Y {};void foo () throw (Y);void bar () throw (Y);void off () throw (X,Y);void foo (){   bar ();}void bar (){   off ();}
The compiler would never know while compiling foo that there is a possibility the function bar may throw a X exception. The compiler cannot check for this unless it follows the call trace until the point of origin for each and every function that is called from foo.Also consider:
void throwsExceptions () throw (…);bool neverThrows () throw (){   try   {	throwsExceptions ();   }   catch(…)   {      return false;   }   return true;}
See the point? Not allowing the call to throwsExceptions in the above codewould make it impossible to write such exception-safe functions altogether.

I hope I have convinced you!

I’m afraid I cannot help you with the lint question, because I have never used one and am not aware if there are products that do such checking.

Your comment that exception specifications don’t have much use is true toan extent. You see, it is very difficult to cope with changes toheader files during development, and the specifications on exceptionstend to keep growing. I know at least one author who advises the avoidance of exceptions. My rule of thumb has always beento specify exceptions thrown only in cases where the class in questionbelongs to a reuseable library. In other words, if the function has a lotof clients, I specify exceptions; otherwise, I don’t.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may