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: 
If the data added to the exception class is only used to display an error message, you could use string concatenation to reduce the number of `push_back()` used. 

For example, you could use:

```
ex.VariableList()[0].push_back(string("Pin state: ") + ToString(pin.getPinState());

```

You could even concatenate all the other messages instead of using separate indices (1, 2, 3, 4, etc.) for each. 

Moreover, for each field, you could use a dedicated setter method to feed the appropriate value. For example:

```
ex.VariableList()[0].setPinState(ToString(pin.getPinState()));

```

and then the `"Pin state: "` part should be moved to the place where the error message is *printed*. 

---

Going even further, your exception class could have a dedicated method which accepts all the objects which contribute to the error message, and call that message instead. For example:

```
void MyExceptionClass::setMessage(Pin& pin, CoordinatesData& cd, EncoderPosition& ep) {
    setPinState(ToString(pin.getPinState()));
    // set whatever else you want here
}

```

Moreover, move the `ToString()` part to wherever the message is getting printed, just store the values in the exception class. For example, change the line above to (you need to change the signature accordingly): 

```
setPinState(pin.getPinState());

```

and let the printing logic decide how to convert it to string. A further advantage is that it allows you to print the same message in different formats.
Those textual descriptions should be linked to the exception class inherently, rather than written into each instance as runtime data.

Similarly, all that informational data should be members of the exception class, and you can format it for output as text later (perhaps in a member function of the exception class itself).