Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::logic_error instead of return false

I am looking for someones opinion about the usage of std::logic_error instead of using a complicated list of nested if/elseif return true/false.

I would like to move from a lot of similar function like the one below

bool validate_data(){
    std::vector<int> v; 
    //fill with data
    if( v.find(10) == v.end() ){
       return false;
    } 
    // other checks that return false
}

to

bool validate_data(){
    std::vector<int> v; 
    //fill with data
    if( v.find(10) == v.end() ){
       throw std::logic_error("error message");
    } 
    // other checks that return false
}

and call all this list of functions in a single try-catch block.

Since it is a derived from std::exception probably I don't know if it is a good idea.

Is anyone using like the example below?

Thanks a lot

AFG

like image 901
Abruzzo Forte e Gentile Avatar asked May 15 '12 12:05

Abruzzo Forte e Gentile


People also ask

What does std :: Logic_error mean?

std::logic_errorThis class defines the type of objects thrown as exceptions to report errors in the internal logical of the program, such as violation of logical preconditions or class invariants. These errors are presumably detectable before the program executes.

What throws std:: logic_ error?

std::logic_error It reports errors that are a consequence of faulty logic within the program such as violating logical preconditions or class invariants and may be preventable.


3 Answers

You should only use exceptions for exceptional circumstances. Using (and checking) return values is far more efficient when the chances of it being true or false are non-trivial. Exceptions are more efficient only when the chance of throwing is so small as to out-weigh the cost of the return-value check.

So if the chances of invalid data are very very low, then go with exceptions. Otherwise, the current solution should not only be fine, but slightly more efficient as well (As throwing and handling is relatively expensive).

like image 62
Dennis Avatar answered Oct 17 '22 07:10

Dennis


Only use exceptions for exceptional situations.

Is not finding the value 10 an exceptional situation? Or is it just a normal situation?

Renaming your method from validate_data to is_data_valid makes it much clearer. If the method returns true, it's valid. If it returns false, it isn't. No need to use exceptions for this.

like image 5
Patrick Avatar answered Oct 17 '22 07:10

Patrick


Since your function is called validate_data() I'd only throw an exception if there is any internal error within the function and use true or false to indicate that the function did validate the input, but it was either valid (return true) or invalid (return false).

This will not prevent you from having multiple if() else if() else constructs but it will make the code cleaner and easier to distinguish if the data were invalid or an internal error happened.

try {
  bool valid = validate_data(foo);
  /* process data or break if invalid */
} catch (std::exception &ex) {
  /* internal error happened */
}

as you can see it will make your code longer, IMHO cleaner.

like image 3
dwalter Avatar answered Oct 17 '22 06:10

dwalter