Fog Creek Software
Discussion Board




throw() in C++ function specifier

I just noticed something rather odd when I downloaded log4cpp

void Category::setPriority(Priority::Value priority)
    throw(std::invalid_argument)
{

}

I blinked once or twice and tried to decide if I had accidentally downloaded log4j by accident :) I poked around a bit more, and discovered that the MFC code in VS.NET 2003 has been update with similar syntax. For instance, in CImage:

BOOL Draw(
  HDC hDestDC,
  const RECT& rectDest
) const throw( );

Anyway, I posted the code to the other developers and they all looked at it with equally blank looks. Of course this probably stems from none of us having worked on projects that used exceptions. At any rate, the question is, did I miss some announcement that C++ supported this syntax? I've worked with people over the years who have come from completely different sorts of projects, with varying years of experience, and have never heard about this till now.

For that matter, I went back and read the MSDN articles about how 'new' throws an exception when it fails instead of returning NULL. Have i been living in some sort of black hole? :) Is this a new style of coding that's now expected since MS is using it?

jedidjab79
Friday, August 29, 2003

I read about that syntax in one of the books I use to learn C++ years ago. IIRC it is used to let you know what kind of exceptions te function can throw.

Roel Schroeven
Friday, August 29, 2003

This syntax has been valid (but not required) since exceptions were added to the language. C++ doesn't have checked exceptions, and C# is on this side of the holy war too (no, I don't want to discuss it).

Seems like another case where Java gets credit for introducing something it didn't.


Friday, August 29, 2003

Exception specifications are described in the ISO spec. See 
http://www.accu.org/c++sig/public/ol34/ol34_exceptions.html .

Marc Evelyn
Friday, August 29, 2003

So they've been around for a while .. but why the sudden shift in the MFC code? Has anyone used them in real world projects?

jedidjab79
Friday, August 29, 2003

Short answer is don't use them.

This is a good link as to why:

http://www.gotw.ca/publications/mill22.htm

Mike
Friday, August 29, 2003

It was true in VC++ 6 that the throw specific was ignored (as is allowed, since they are not required), EXCEPT in one very specific condition: the empty throw specification caused the compiler to generate slightly more efficient code on the assumption that an exception would never leave the method.

Moral of the story is: if you put them in there, you better make sure they're right, because you could face some unexpected behavior if they're wrong.

Given that they're not checked like in Java (and thus compiler enforced), then I would say you probably shouldn't use them.

Brad Wilson (dotnetguy.techieswithcats.com)
Friday, August 29, 2003

Ah ok ... good article there :) Thanks for the info.

jedidjab79
Friday, August 29, 2003

This is a year old, and apparently out of date, but bears reading too...

"Next to export, exception specifications are probably the biggest conformance feature missing in Visual C++ .NET. And like export, it's a feature that sane people can argue should never have been in the language to begin with. Indeed, at a talk he gave at Microsoft a couple of months ago, C++ Godfather Bjarne Stroustrup labeled exception specifications a 'failed experiment.' Exception specifications are particularly nefarious because they don't do what many people think they do or want them to do."

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndeepc/html/deep08272002.asp

I'm unusually busy at the moment, and haven't tracked this whole issue down yet.  (Since I'm saddled with VC6 right now, it's not that important to me - yet).

Grumpy Old-Timer
Friday, August 29, 2003

On the subject of 'new' throwing an exception, now's the perfect time to tell you to go learn about Resource Acquisition Is Initialization (RAII). http://sourceforge.net/docman/display_doc.php?docid=8673&group_id=9028 is a pretty good intro, or google for it ( http://www.google.com/search?q=resource+acquisition+is+initialization ).

And yes, if you try to write C++ without knowing about this, then you have been living under at least a small rock. :)

Exception guy
Friday, August 29, 2003

> the empty throw specification caused the compiler to
> generate slightly more efficient code on the assumption
> that an exception would never leave the method.

Actually, I have read the opposite. If a function says it will not throw any exceptions (i.e. throw()), then the compiler would add extra code to the function to verify that at RUNTIME. The C++ standard says that if a throw() function actually throws an exception, then the program must call the terminate() function. C++ exceptions are checked dynamically, not statically.

runtime
Friday, August 29, 2003

Exception guy : :)  However, being stuck in mostly a MS world, their implementation of new hasn't (and still doesn't) throw an exception when it fails. They do mention in the 2003 docs that the 'standard' new does, and you have to call new(nothrow) otherwise --- BUT it also forces you to link against the non-default library. So, in the end, we're still using new that returns NULL as far as I can see.

jedidjab79
Friday, August 29, 2003

This is just one thing that Java has over C++.  The Java
language (for better or worse) is what Sun says it is.  On
the other hand, C++ is the accumulation of several layers
of committee decisions, with the compiler authors playing
catch-up for exceptions, templates, STL, etc.  The result
being that developers are forced to skip most of the modern
stuff if they need to have portable code.

jeffh
Saturday, August 30, 2003

"Of course this probably stems from none of us having worked on projects that used exceptions."

Whoa.

"For that matter, I went back and read the MSDN articles about how 'new' throws an exception when it fails instead of returning NULL. Have i been living in some sort of black hole? :)"

Whoa again.

Exception specifications are rather useless though; they're not a contract, so what's the point?

Jason McCullough
Tuesday, September 09, 2003

*  Recent Topics

*  Fog Creek Home