Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do functions inform user that it throws exception based on function prototype?

I went through several discussion, tutorials etc. and I've a feeling like there is no way to inform the user using prototype, that his function might throw an exception.

For instance:

/* AudioStream.h */
class AudioStream
{
   int open(struct stream_settings &settings);
}
/* AudioStream.cpp */
int AudioStream::open(struct stream_settings &settings)
{
    int err;
    err = snd_pcm_open(...);
    if (err < 0)
    {
        /* Throw some exception here */
    }
}

If the final product ends up in a library with a header. How does one figures out, that the open function throws an exception and it's necessary to put it into a try/catch block?

Thank you for all the great answers.

like image 209
ST Renegade Avatar asked Feb 09 '26 22:02

ST Renegade


1 Answers

Unlike some languages which indicate in the function signature that it "throws", C++ has no such mechanism. This is something you must establish in the documentation or comments near the function definition.

like image 195
tadman Avatar answered Feb 15 '26 02:02

tadman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!