Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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)

share|improve this question

marked as duplicate by MichaelT, GlenH7, gnat, Bart van Ingen Schenau, Kilian Foth May 6 at 9:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
I'd also suggest poking around Arguments for or against using Try/Catch as logical operators, Dont Use Exceptions For Flow Control, Why not use exceptions as regular flow of control? (and lots of linked questions to that last one). –  MichaelT May 4 at 1:37
    
@MichaelT so I guess your suggestion is that trying to eliminate a possible programmer error is not valid reason to use exception? –  Bryan Chen May 4 at 1:56

1 Answer 1

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.

share|improve this answer
    
I don't understand your answer. How does it apply to my specific case? –  Bryan Chen May 4 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 May 4 at 3:15
    
so you basically saying if throw exception is not required, don't throw exception –  Bryan Chen May 4 at 3:30

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