C

QSafeExceptionHandler Class

class SafeRenderer::QSafeExceptionHandler

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

Header: #include <QSafeExceptionHandler>
Since: QtSafeRenderer 3.0

Static Public Members

void handleException(const T &exception)
void installExceptionHandler(SafeRenderer::QSafeExceptionHandlerInterface *exceptionHandlerInterface)

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::terminate if exceptions are disabled.

Member Function Documentation

[static] template <typename T> 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::terminate 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.

If if the QSafeExceptionHandlerInterface::onException function call returns true, QSR will not handle the said exception and it is up to the user to make sure the system is in operable state. Usually when exception is thrown and caught in the exception handler, running the application should be terminated.

This exception handler allows the user / system to add needed logging and tear-down logic before system is shut down / rebooted. In case the system does not support exceptions, using exception handler can be used in combination with defining QT_NO_EXCEPTIONS flag, in which case std::terminate is called unless user handles the exception in the exception handler.

Example of an exception handler that catches only SafeRenderer::QSafeFileExceptions and lets QSR handle everything else:

// Our main class can inherit the QSafeExceptionHandlerInterface and so can be installed
// as an exception handler.
class SafeSystem : public SafeRenderer::QSafeExceptionHandlerInterface {
public:
    virtual bool onException(const SafeRenderer::QSafeException &exception) {
        bool exceptionHandled = false;

        // Only handle FileExceptions, let SafeRenderer handle everything else.
        if (exception.type() == SafeRenderer::QSafeException::ExceptionType::FileException) {
            // Log the file exception.
            std::cerr << "File exception caught: " << exception.what();

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

            exceptionHandled = true;
        }

        // Return false if SafeRenderer needs to handle this exception, true if we handled it.
        return exceptionHandled;
    }
};

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 and can be intercepted or let fall through.
    SafeRenderer::QSafeExceptionHandler::installExceptionHandler(&system);

    // Rest of the QSR initialization...
}

Available under certain Qt licenses.
Find out more.