1

I know that exception should be thrown in exceptional case (e.g. out of memory, programmer error). For these cases, I don't need to worry about performance throwing these exception.

But what happen if use exception in normal code path?

In my case, use it to stop user code supplied function.

So for given code, is foo better or foo2 better?

It is very likely that outputFunc will return false / throw exception.

// library provided function
void foo(std::function<void(std::function<bool(int)>)>);

// how user suppose to use it
foo([](std::function<bool(int)> outputFunc){
    while (/*condition*/)
    {
        // user have to check the return value and stop work when it return false
        if (!outputFunc(/*some value*/)) return; 
    }
});

// library provided function
void foo2(std::function<void(std::function<void(int)>)>);

// how user suppose to use it
foo2([](std::function<void(int)> outputFunc){
    while (/*condition*/)
    {
       // this will throw exception and handled internally when it should stop
       // user does not have to check the return value but they need to write exception-safe code
        outputFunc(/*some value*/));
    }
});

In my case, the use of exception eliminate a possible programmer error (by not checking return value of outputFunc) and make code easier to write (no need to check return value)

And the exception is throw/catch internally, no user code required to deal with them (except they need to write exception-safe code in the lambda, but we should always write exception-safe code)

2

1 Answer 1

0

If a function cannot do what it is supposed to do and returning a value will be misleading, then throwing an exception seems to be the only other option.

This situation ideally would never happen, and if it does it should represent a problem in the system's environment or bug in the code. Ideally you should try to work around it by doing some prevalidation yourself. For example call some function to check whether the inputs/state is valid for the function you really want to call. That validation function would return a boolean, and depending on that result, you will know whether or not to call the intended function.

3
  • I don't understand your answer. How does it apply to my specific case?
    – Bryan Chen
    Commented May 4, 2014 at 3:10
  • If you are using the exception just as flow control (i.e. where an "if" would work instead), then I don't think it should be used (similar idea to the links that MichaelT provided). Exceptions, I believe, should only be used when "a function cannot do what it is supposed to do (i.e. due to invalid state or arguments, or an unacceptable response from a dependency is returned) and returning a value will be misleading". If you can work around the problem by first checking whatever precondition is failing, i.e. with an "if" statement, then I believe that should be done instead.
    – jordan
    Commented May 4, 2014 at 3:15
  • so you basically saying if throw exception is not required, don't throw exception
    – Bryan Chen
    Commented May 4, 2014 at 3:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.