Skip to content

Error message context manager#

superqt.utils.exceptions_as_dialog #

Bases: AbstractContextManager

Context manager that shows a dialog when an exception is raised.

See examples below for common usage patterns.

To determine whether an exception was raised or not, check the exception attribute after the context manager has exited. If use_error_message is False (the default), you can also access the dialog attribute to get/manipulate the QMessageBox instance.

Parameters:

Name Type Description Default
exceptions type[BaseException] | tuple[type[BaseException], ...]

The exception(s) to catch, by default Exception (i.e. all exceptions).

Exception
icon Icon

The icon to show in the QMessageBox, by default QMessageBox.Icon.Critical

Critical
title str

The title of the QMessageBox, by default "An error occurred".

'An error occurred'
msg_template str

The message to show in the QMessageBox. The message will be formatted using three variables:

  • exc_value: the exception instance
  • exc_type: the exception type
  • tb: the traceback as a string

The default template is the content of the exception: "{exc_value}"

'{exc_value}'
buttons StandardButton

The buttons to show in the QMessageBox, by default QMessageBox.StandardButton.Ok

Ok
parent QWidget | None

The parent widget of the QMessageBox, by default None

None
use_error_message bool | QErrorMessage

Whether to use a QErrorMessage instead of a QMessageBox. By default False. QErrorMessage shows a checkbox that the user can check to prevent seeing the message again (based on the text of the formatted msg_template.) If True, the global QMessageError.qtHandler() instance is used to maintain a history of dismissed messages. You may also pass a QErrorMessage instance to use a specific instance. If use_error_message is True, or if you pass your own QErrorMessage instance, the parent argument is ignored.

False

Attributes:

Name Type Description
dialog QMessageBox | None

The QMessageBox instance that was created (if use_error_message was False). This can be used, among other things, to determine the result of the dialog (e.g. dialog.result()) or to manipulate the dialog (e.g. dialog.setDetailedText("some text")).

exception BaseException | None

Will hold the exception instance if an exception was raised and caught.

Examplez#

from qtpy.QtWidgets import QApplication
from superqt.utils import exceptions_as_dialog

app = QApplication([])

with exceptions_as_dialog() as ctx:
    raise Exception("This will be caught and shown in a QMessageBox")

# you can access the exception instance here
assert ctx.exception is not None

# with exceptions_as_dialog(ValueError):
#     1 / 0  # ZeroDivisionError is not caught, so this will raise

with exceptions_as_dialog(msg_template="Error: {exc_value}"):
    raise Exception("This message will be inserted at 'exc_value'")

for _i in range(3):
    with exceptions_as_dialog(AssertionError, use_error_message=True):
        assert False, "Uncheck the checkbox to ignore this in the future"

# use ctx.dialog to get the result of the dialog
btns = QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel
with exceptions_as_dialog(buttons=btns) as ctx:
    raise Exception("This will be caught and shown in a QMessageBox")
print(ctx.dialog.result())  # prints which button was clicked

app.exec()  # needed only for the use_error_message example to show