Actually, I do understand major pros and cons of using exceptions. And I use them in my projects by default as error-handling strategy. But now I'm starting a Windows CE project with Qt library, and I see that Qt creators refused to use exceptions in the class hierarchy.
So, if I use exceptions I will need to carefully translate them to and from error codes (or some objects, or just swallow) on my/Qt code bounds. Otherwise, I can refuse to use exceptions in my code and switch to some other strategy.
What would be the best error handling strategy in my case - to use exceptions or to use error-codes, or etc...? Do you have experience with Qt development and what error handling strategy did you use?
Currently, the only supported use case for recovering from exceptions thrown within Qt (for example due to out of memory) is to exit the event loop and do some cleanup before exiting the application. Typical use case: QApplication app(argc, argv); ... int ret; try { ret = app.
An exception handling strategy consists of all actions and conventions necessary to properly handle exceptions in your application. The following diagram shows an overview of the various parts of an exception handling strategy, from the error is detected, all the way up to where the error is handled.
Both exceptions and errors are the subclasses of a throwable class. The error implies a problem that mostly arises due to the shortage of system resources. On the other hand, the exceptions occur during runtime and compile time.
Override QApplication::notify() and handle exceptions there (not 100% on the return value). You can "throw" exceptions from signal handlers but they don't get propagated to Qt in this way.
bool
notify(QObject * rec, QEvent * ev)
{
try
{
return QApplication::notify(rec,ev);
}
catch(my::Exception & e)
{
QMessageBox::warning(0,
tr("An error occurred"),
e.message());
}
catch(...)
{
QMessageBox::warning(0,
tr("An unexpected error occurred"),
tr("This is likely a bug."));
}
return false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With