radioviz.services.exception_handler
Exception handling utilities for Qt applications.
This module provides functions for handling exceptions in a user-friendly way by displaying them in dialog boxes rather than crashing the application or only logging to console.
Thanks to the ThreadExceptionHandler, exceptions originating from worker threads are transferred to the main thread via the thread-safe Qt messaging system and finally handled in the main thread.
In order for this to work, the worker thread must be structured in this way:
class ExceptionTestThread(QThread):
def run(self):
try:
print('about to raise')
raise Exception('Test exception from thread')
except Exception:
print('caught in thread')
# Get the thread exception handler and pass the exception info
from radioviz.exception_handler import (
get_thread_exception_handler,
)
handler = get_thread_exception_handler()
handler.handle_exception(*sys.exc_info())
Functions
Get or create the global thread exception handler. |
|
Install a global exception handler to show errors in a dialog box. |
|
|
Display an exception in a message box dialog. |
Classes
A handler for exceptions that occur in threads other than the main thread. |
- class radioviz.services.exception_handler.ThreadExceptionHandler[source]
Bases:
QObjectA handler for exceptions that occur in threads other than the main thread.
This class provides a way to safely handle exceptions that occur in QThreads by passing them to the main thread where they can be properly displayed in a QMessageBox.
- handle_exception(exc_type: type[BaseException] | None, exc_value: BaseException | None, exc_traceback: TracebackType | None) None[source]
Handle an exception by emitting a signal to the main thread.
- Parameters:
exc_type (Type[BaseException] | None) – Type of the exception
exc_value (Exception) – The exception instance
exc_traceback (TracebackType | None) – Traceback object
- show_exception_dialog(exc_type: type[BaseException] | None, exc_value: BaseException | None, exc_traceback: TracebackType | None) None[source]
Display an exception in a message box dialog (called in main thread).
- Parameters:
exc_type (Type[BaseException] | None) – Type of the exception
exc_value (Exception) – The exception instance
exc_traceback (TracebackType | None) – Traceback object
- radioviz.services.exception_handler.get_thread_exception_handler() ThreadExceptionHandler[source]
Get or create the global thread exception handler.
- Returns:
The global thread exception handler instance
- Return type:
- radioviz.services.exception_handler.install_exception_handler() Callable[[type[BaseException], BaseException, TracebackType | None], Any][source]
Install a global exception handler to show errors in a dialog box.
This function replaces the default Python exception handler with the custom implementation that displays errors in a graphical dialog. It should be called after QApplication is created.
- Returns:
The original exception handler function
- Return type:
callable
- radioviz.services.exception_handler.show_exception_dialog(exception_type: type[BaseException] | None, exception_value: BaseException | None, exception_traceback: TracebackType | None) None[source]
Display an exception in a message box dialog.
This function creates and displays a dialog box showing details of an exception that occurred during program execution.
- Parameters:
exception_type (Type[BaseException] | None) – Type of the exception
exception_value (Exception) – The exception instance
exception_traceback (TracebackType | None) – Traceback object
- radioviz.services.exception_handler._thread_exception_handler: ThreadExceptionHandler | None = None
The global instance of the thread exception handler