Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Throw exceptions in constructor

I have created a class that has as members 3 cv::Ptrs:

DetectorPtr m_detector;
ExtractorPtr m_extractor;
MatcherPtr m_matcher;

And in the constructor I create them based on strings from an input configFile (that is a class based on a boost::ptree; but this does not matter too much). If the members are not created (they are empty) I throw exceptions:

SolutionPOI::SolutionPOI(const ConfigFile& configFile, const Displayer& displIn)
    :   m_solPOIConfigFile(configFile),
        m_displayer(displIn)
{
    // initialize the feature detector
    m_detector = cv::FeatureDetector::create(m_solPOIConfigFile.getPOIFeatureDetectorType());
    if (m_detector.empty())
    {
        throw POIException(
            "Feature detector of type " + m_solPOIConfigFile.getPOIFeatureDetectorType() + " was not created!");
    }
    // initialize the descriptors exctractor
    m_extractor = cv::DescriptorExtractor::create(m_solPOIConfigFile.getPOIDescriptorsType());
    if (m_extractor.empty())
    {
        throw POIException(
            "Descriptors exctractor of type " + m_solPOIConfigFile.getPOIDescriptorsType() + " was not created!");
    }
    // initialize the descriptors matcher
    m_matcher = cv::DescriptorMatcher::create(m_solPOIConfigFile.getPOIMatcherType());
    if (m_matcher.empty())
    {
        throw POIException(
            "Descriptors matcher of type " + m_solPOIConfigFile.getPOIMatcherType() + " was not created!");
    }
}

I know that it is not nice to throw in the constructor, do you suggest me any other way (like there is no need to do that for a reason)?