Question

Can one explain me the syntax of the following expression found in utsname.h (utsname definition):

extern int uname (struct utsname *__name) __THROW;

__THROW is a macro and gives throw() from cdefs.h. Why can it be placed like this in the definition of an extern function?

Was it helpful?

Solution

Declaring a function as throw means that no exceptions will be thrown when using this function.

Copied from http://www.cplusplus.com/doc/tutorial/exceptions/

Exception specifications

When declaring a function we can limit the exception type it might directly or indirectly throw by appending a throw suffix to the function declaration:

float myfunction (char param) throw (int);

This declares a function called myfunction which takes one argument of type char and returns an element of type float. The only exception that this function might throw is an exception of type int. If it throws an exception with a different type, either directly or indirectly, it cannot be caught by a regular int-type handler.

If this throw specifier is left empty with no type, this means the function is not allowed to throw exceptions. Functions with no throw specifier (regular functions) are allowed to throw exceptions with any type:

int myfunction (int param) throw(); // no exceptions allowed
int myfunction (int param);         // all exceptions allowed 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top