I have the following situation.
I'm writing a c++ program where I have a function that checks a custom equivalence of two json objects. If the check fails, I don't want the entire program to stop (its a QT gui program), and I have a branch that executes upon receiving an exception. This exception is then passed up to QT where a the qt gui displays an error message from e.what()
.
In order to do this I made a function that took care of my exception throwing for the equivalence comparisons. it looks something like this:
void makeComparison(const json& a, const json& b){
if(condition fails)
throw (error)
if(other condition fails)
throw (error)
...
}
I debated just making this a boolean function instead, however I wanted verbose error messages to tell exactly what went wrong in the program to the user. It seems odd to have a function that doesn't do anything but throw errors on comparison failures, and then having to catch those and keep throwing them upward. I feel like there's got to be a better way to handle this. Here is ME of what the rest of the program looks like.
// some member function
try{
makeComparison(a, m_b);
// do something
}catch (exception e){
// do soemthing else
throw(e);
}
// in gui
try{
object.exceptionThrowingCode(a);
}catch (exception e){
QMessageBox::critical(this, tr("Error"), tr(e.what());
}
goto
. It may interest – Laiv May 17 '17 at 18:21makeComparison
and its caller. – Useless May 18 '17 at 12:29throw(e);
is used instead ofthrow;
. – Sjoerd May 24 '17 at 02:15// do soemthing else
does. If - as is often the case - it's cleanup code, you can simply replace all thetry/catch/throw
framework with proper use of RAII. – Useless May 30 '17 at 15:47