SafeRenderer::QSafeExceptionHandler Class | Qt Safe Renderer Runtime C++ Classes (original) (raw)

SafeRenderer::QSafeExceptionHandler Class

class SafeRenderer::QSafeExceptionHandler

The QSafeExceptionHandler class is a proxy class to deliver or throw exceptions. More...

Header: #include <qsafeexceptionhandler.h>
Since: QtSafeRenderer 2.2

Static Public Members

Detailed Description

Exception handler base class that can be called to throw an exception, let the user handle the exception through exception handler or call std::abort if exceptions are disabled.

Member Function Documentation

[static] template void QSafeExceptionHandler::handleException(const T &exception)

Static function to determine what to do with given exception. If QT_NO_EXCEPTIONS is defined and no user provided exception handler is installed (or user did not handle the exception), std::abort will be called.

Default behavior is to throw the given exception.

Type of the argument needs to be SafeRenderer::QSafeException or derived from it.

[static] void QSafeExceptionHandler::installExceptionHandler(SafeRenderer::QSafeExceptionHandlerInterface *exceptionHandlerInterface)

Install a filter class exceptionHandlerInterface which is called for each incoming SafeRenderer::QSafeException.

The QSafeExceptionHandlerInterface::onException function will be called before the exception is thrown or std::abort is called. After the exception handler is invoked, QSR will always either throw the exception (if QT_NO_EXCEPTIONS is not defined) or call std::abort (if QT_NO_EXCEPTIONS is defined).

This exception handler allows the user / system to add needed logging and tear-down logic before the exception is thrown or the system is shut down / rebooted. When an exception is thrown, running the application should usually be terminated.

Example of an exception handler that logs SafeRenderer::QSafeFileExceptions:

// Our main class can inherit the QSafeExceptionHandlerInterface and so can be installed
// as an exception handler.
class SafeSystem : public SafeRenderer::QSafeExceptionHandlerInterface {
public:
    virtual void onException(const SafeRenderer::QSafeException &exception) {
        // Log file exceptions.
        if (exception.type() == SafeRenderer::QSafeException::ExceptionType::FileException) {
            std::cerr << "File exception caught: " << exception.what();

            // Any other logic for handling file exceptions...
        }

        // After this function returns, the exception will be thrown or std::abort will be called.
    }
};

int main(int argc, char *argv[])
{
    Q_UNUSED(argc);
    Q_UNUSED(argv);

    // Create instance of our system.
    SafeSystem system;

    // Install the system as our exception handler. Now all exceptions will pass through
    // our system's onException() function before being thrown or causing abort.
    SafeRenderer::QSafeExceptionHandler::installExceptionHandler(&system);

    // Rest of the QSR initialization...
}

Available under certain Qt licenses.
Find out more.