Problem:
I'm trying to figure out the best method to handle unhandled exceptions that usually get suppressed by PyQt.
My solution right now is to decorate appropriate methods with the following decorator
def handleExceptions(func):
"""
Decorator: create and show message box for every unhandled exception that
reaches the decorator
"""
@functools.wraps(func)
def decorator(self):
try:
func(self)
except Exception as error:
# ...
if errBox.exec_() == QMessageBox.Ok:
try:
self.closeApp()
except AttributeError:
exit()
return decorator
However this solution only works if none of the decorated methods require more parameters than self
. I tried extending the decorator like this
...
def decorator(self, *args)
try:
func(self, *args)
...
This allows calling functions with one or more parameters (including self
). Unfortunately this code breaks access to methods which require no parameter except self
which fail with the message TypeError: method() takes 1 positional argument but 2 were given
.
After using a debugger I figured out that the second parameter in *args
seems to be a bool (False
).
Questions:
- Why does this additional parameter get passed?
- How can the decorator be modified to allow usage with functions of varying parameter lengths?
- Is this even a good way to handle exceptions? I would be grateful if someone could tell me the usual method to deal with unhandled exceptions in PyQt.
I looked up other solutions like this one (which I don't fully understant) and tried to implement them without success (it either throws exceptions like my one or breaks the decorated methods completely).
Thanks in advance!