1

When using VC++ 2008 and 2010, which marker is better to use to indicate a function won't throw exceptions:

  • throw() (C++ standard)
  • __declspec(nothrow) (MS extension)

I read a few older forum discussions where people said that using throw() may actually force the compiler to generate additional code to catch exceptions in case the function does throw (against the marker). Their advice is not to use throw() but use __declspec(nothrow) instead, since the compiler can actually use it for optimization.

I did some searches but I couldn't come up with really useful results. From how I understand, the Boost library advises against using them here. __declspec(nothrow) is non-standard C++, so if MS implements exception specifications, it'll keep working the same way while the throw() behavior may change.

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • 4
    Related: [Is there any use for C++ throw decoration?](http://stackoverflow.com/questions/1410226/is-there-any-use-for-c-throw-decoration), [Throw keyword in function's signature (C++)](http://stackoverflow.com/questions/1055387/throw-keyword-in-functions-signature-c), [What is the point of `void func() throw(type)`?](http://stackoverflow.com/questions/1589459/what-is-the-point-of-void-func-throwtype), [Why aren't exceptions in C++ checked by the compiler?](http://stackoverflow.com/questions/1037575/why-arent-exceptions-in-c-checked-by-the-compiler). Personally, I'd say "neither". – Cody Gray - on strike Jan 27 '12 at 21:16
  • 2
    Exception specification isnt very useful in c++: http://www.gotw.ca/publications/mill22.htm –  Jan 27 '12 at 21:21
  • @CodyGray Thanks, I was searching for nothrow and __declspec, I couldn't find much that way. – xxbbcc Jan 27 '12 at 21:37
  • CodyGray: Please note that all those questions are now dated by a new standard, that significantly changes the allowed optimizations. – Ben Voigt Jan 27 '12 at 21:39
  • Related: [Why add a throw() to your methods?](http://blogs.msdn.com/b/larryosterman/archive/2006/03/22/558390.aspx). The comments in this blog post are worth reading as well. –  May 07 '12 at 14:20

1 Answers1

2

The standard form is noexcept, but VC++ 2008 and 2010 don't support this.

Personally, I'd use a macro, defined as throw() (or maybe even nothing) until compilers start supporting C++11 noexcept, and then change it.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    Thanks for the answer - I chose to use __declspec(nothrow) since the VC++ compiler can use that to actually optimize away unneeded exception code. – xxbbcc Jan 29 '12 at 15:21