I tried googling it but all results were about C++’s throw std::exception()
.
I was reading through libraries (was curious how stuff like printf
, malloc
and FILE
were implemented) and came across the definition for the malloc
function:
extern void *malloc (size_t __size) __THROW __attribute_malloc__
__attribute_alloc_size__ ((1)) __wur;
When using the IDE (Visual Studio Code) to trace back to definitions for each thing, __THROW
led to this:
# if !defined __cplusplus && __GNUC_PREREQ (3, 3)
// stuff that doesn't happen
# else
# if defined __cplusplus && __GNUC_PREREQ (2,8)
# define __THROW throw ()
# define __THROWNL throw ()
# define __NTH(fct) __LEAF_ATTR fct throw ()
# define __NTHNL(fct) fct throw ()
// continuation to the if-else macro
This confused me, as, as far as i know, c doesn’t have exceptions and instead uses int error codes. Even more, why are there parentheses as in a function call?
What does it mean and what does it do in the presented case?
>Solution :
There is no throw
keyword in C. That’s a C++ thing.
As to why you find it in your code, the clue is right here in the preprocessor macros:
# if !defined __cplusplus && __GNUC_PREREQ (3, 3)
// stuff that doesn't happen
# else
# if defined __cplusplus && __GNUC_PREREQ (2,8)
# define __THROW throw ()
# define __THROWNL throw ()
# define __NTH(fct) __LEAF_ATTR fct throw ()
# define __NTHNL(fct) fct throw ()
// continuation to the if-else macro
This code is written so it will compile with either a C or C++ compiler. The C++ bit that references throw
and other C++isms will only be compiled if a C++ compiler is used, as such a compiler will define the __cplusplus
macro.
When you compile it with a C compiler, only the bit marked // stuff that doesn't happen
will be used, not the else block.