Is throwing an exception a healthy way to exit?

Posted by ramaseshan on Stack Overflow See other posts from Stack Overflow or by ramaseshan
Published on 2010-05-20T08:25:35Z Indexed on 2010/05/20 8:30 UTC
Read the original article Hit count: 187

Filed under:
|
|

I have a setup that looks like this.

class Checker
{   // member data
    Results m_results; // see below
 public:
    bool Check();
 private:
    bool Check1();
    bool Check2();
    // .. so on
};

Checker is a class that performs lengthy check computations for engineering analysis. Each type of check has a resultant double that the checker stores. (see below)

bool Checker::Check()
{   // initilisations etc.
    Check1();
    Check2();
    // ... so on
}

A typical Check function would look like this:

bool Checker::Check1()
{   double result;
    // lots of code
    m_results.SetCheck1Result(result);
}

And the results class looks something like this:

class Results
{   double m_check1Result;
    double m_check2Result;
    // ...
public:
    void SetCheck1Result(double d);
    double GetOverallResult()
    { return max(m_check1Result, m_check2Result, ...); }
};

Note: all code is oversimplified.

The Checker and Result classes were initially written to perform all checks and return an overall double result. There is now a new requirement where I only need to know if any of the results exceeds 1. If it does, subsequent checks need not be carried out(it's an optimisation). To achieve this, I could either:

  • Modify every CheckN function to keep check for result and return. The parent Check function would keep checking m_results. OR
  • In the Results::SetCheckNResults(), throw an exception if the value exceeds 1 and catch it at the end of Checker::Check().

The first is tedious, error prone and sub-optimal because every CheckN function further branches out into sub-checks etc.

The second is non-intrusive and quick. One disadvantage is I can think of is that the Checker code may not necessarily be exception-safe(although there is no other exception being thrown anywhere else). Is there anything else that's obvious that I'm overlooking? What about the cost of throwing exceptions and stack unwinding?

Is there a better 3rd option?

© Stack Overflow or respective owner

Related posts about c++

Related posts about exceptions