Question: My problem is that I'm writing a program which is supposed to be readable for the future, and the program has a lot of exception cases. So whenever I have to throw an exception, I have to writer over 10 lines to initialize my exception class and append the information from the program to it. For example as follows:

```
MyExceptionClass ex;
ex.setErrorMessage("PIN_CANNOT_GO_IN");
ex.setErrorDetails("The pin is asked to go to the state IN while the depth of the r-coordinate does not support it");
ex.setResolutionMessage("Configure your coordinates-file to move first to the correct position before changing the state of the pin");
ex.VariableList().resize(5);
ex.VariableList()[0].push_back("Pin state: ");
ex.VariableList()[0].push_back(ToString(pin.getPinState()));
ex.VariableList()[1].push_back("Pin target state: ");
ex.VariableList()[1].push_back(ToString(coordinatesData[coordinatesIndex].targetPinState));
ex.VariableList()[2].push_back("Current r Value: ");
ex.VariableList()[2].push_back(ToString(EncoderPosition.r));
ex.VariableList()[3].push_back("Current phi Value: ");
ex.VariableList()[3].push_back(ToString(EncoderPosition.phi));
ex.VariableList()[4].push_back("Current z Value: ");
ex.VariableList()[4].push_back(ToString(EncoderPosition.z));

ex.printLog();
ex.writeLog(exceptionLogFilePath.getValue());

throw ex;

```

So for only 5 variables, I had to write all that...
Is there an efficient way to contain all information from the program (variables at least) and not rewrite all this every time I wanna throw an exception?

Thanks in advance.

Answer: 
You could use a common function (fill\_out\_exception\_parameters) that fills out the VariableList object for a generic exception and re-use that in any new exception classes that you write
I think I got the cleanest way to do it. Please let me hear what you think.

So I encapsulate all the relevant variables in a templatized class as follows (just a quick-and-dirty example)

```
class VarBase
{
VarBase();
static std::vector<VarBase*> __allParams;
string getStringValue() = 0;
};

template <typename T>
class Var : public VarBase
{
    T value;
    string name;
    string description;
    toString();
    operator T();
    string getStringValue();
};

VarBase::VarBase()
{
    __allParams.push_back(this);
}
VarBase::~VarBase()
{
    //handle removing from __allParams vector or whatever container
}
template <typename T>
std::string Var<T>::getStringValue()
{
    std::stringstream s;
    s << paramValue;
    return s.str();
}

```

Now if the my exception class is friends with the VarBase class, it can access \_\_allParams and loop through that and call getStringValue() which will automatically do convert the value to a string and add it to my exception calss when necessary :)

Any additional ideas is highly appreciated.