简体   繁体   English

所有的OpenCV函数都抛出异常吗?

[英]Does all OpenCV functions throw exceptions?

OpenCV throws exceptions when hitting critical errors , however, in the documentation it is rarely (dare I say never) mentioned whether a particular function throws. OpenCV在遇到严重错误时会引发异常 ,但是,在文档中很少(敢说我从来没有)提到过某个特定函数是否引发。 See for instance the documentation on the watershed algorithm 例如,请参阅分水岭算法文档

I would like to catch errors close to where they occur and, for instance, wrap the call to watershed in a try-catch. 我想在错误发生的地方捕获错误,例如,将对分水岭的调用包装在try-catch中。 But the documentation does not state whether the function throws, so wrapping it in try-catch may be a false security. 但是文档没有说明函数是否抛出,因此将其包装在try-catch中可能是错误的安全性。

Can I assume that all OpenCV functions throws exceptions? 我可以假定所有OpenCV函数都抛出异常吗?

Can I assume that all OpenCV functions throws exceptions? 我可以假定所有OpenCV函数都抛出异常吗?

For the most part, yes. 多半是对的。 Many OpenCV methods will most likely perform a CV_ASSERT which will call cvError . 许多OpenCV方法很可能会执行CV_ASSERT ,它将调用cvError In turn, cv::error will log, and throw exception. 反过来, cv::error将记录并抛出异常。

But the documentation does not state whether the function throws, so wrapping it in try-catch may be a false security. 但是文档没有说明函数是否抛出,因此将其包装在try-catch中可能是错误的安全性。

My rule-of-thumb is that if the method returns void , and the method is manipulative and/or acts on a resource, then it's wise to wrap in a try-catch. 我的经验法则是,如果该方法返回void ,并且该方法是可操作的和/或对资源起作用的,则将try-catch包裹起来是明智的。 When in doubt, I'll evaluate the source code of the method to see what the assertions are checking for. 如有疑问,我将评估该方法的源代码,以查看断言要检查的内容。

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

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