简体   繁体   English

std :: logic_error而不是return false

[英]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. 我正在寻找某人对std::logic_error的使用的std::logic_error而不是使用嵌套的if / elseif的复杂列表返回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. 并在一个try-catch块中调用所有这些函数列表。

Since it is a derived from std::exception probably I don't know if it is a good idea. 因为它是从std::exception派生的,所以我不知道这是不是一个好主意。

Is anyone using like the example below? 是否有人使用如下例子?

Thanks a lot 非常感谢

AFG AFG

Only use exceptions for exceptional situations. 仅在特殊情况下使用例外。

Is not finding the value 10 an exceptional situation? 是不是发现价值10是一种特殊情况? Or is it just a normal situation? 还是只是一个正常的情况?

Renaming your method from validate_data to is_data_valid makes it much clearer. 将您的方法从validate_data重命名为is_data_valid使其更清晰。 If the method returns true, it's valid. 如果方法返回true,则它是有效的。 If it returns false, it isn't. 如果它返回false,则不是。 No need to use exceptions for this. 不需要为此使用例外。

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). 否则,当前的解决方案不仅应该是好的,而且还应该稍微更高效(因为投掷和处理相对昂贵)。

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 ). 因为你的函数叫做validate_data()所以如果函数中有任何内部错误,我只会抛出一个异常,并使用truefalse表示函数确实验证了输入,但是它validreturn true )或无效( 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. 这不会阻止您使用多个if() else if() else构造,但如果数据无效或发生内部错误,它将使代码更清晰,更容易区分。

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. 正如你所看到它会使你的代码更长,恕我直言更清洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM