There are several differences:
The __attribute__((...))
syntax is a gnu compiler extension, which is not exactly portable, throw()
is defined by the C++ standard.
If I read it correctly, __attribute__((nothrow))
tells the compiler that it can safely assume that a function won't raise an exception, and can possibly omit emitting some code necessary for exception handling (this is on the caller side). throw()
, on the other hand, implicitely catches all exceptions that arise from the function in question, and terminates the program when something is caught by calling first the unexpected exception handler, which by default calls terminate()
(this happens on the callee side).
In terms of programming, throw()
is the much more useful, because it can safeguard your code from being silently skipped by an exception. When I work on a project that uses exceptions, I make a point to add throw()
to every single function I write.
Note, however, that neither __attribute__((nothrow))
nor throw()
will cause the compiler to actually check whether no exception can be thrown. This is quite unfortunate, but the C++ standard explicitely says that the compiler must not throw an error on a throw within a function declared with throw()
.