Search code examples
visual-c++exceptionthrowdeclspec

VC++ 2008/2010: is throw() or __declspec(nothrow) a better choice?


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.


Solution

  • 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.