Strategy for handling exceptions in property accessors
-
Hi all!
I'm curious what strategy you use to handle exceptions in property accessors, specifically those in Python/QML applications. The specific situation is that I have a property like
@Property(int, notify=thing_changed) def thing(self): return self._some_object.get()
If
self._some_object.get()
raises an exception, the application crashes without logging any kind of error. You really have no choice except to figure out which property is the problem and when it gets accessed. It would be much, much easier if a proper traceback were available.I'm not even sure what is handling that exception... is it the python interpreter? Is it the QML engine? My guess is that it is not the python interpreter, as I have an exception hook registered and a logger configured. I could be wrong. Is there a way to configure the QML engine to handle it? Or is my only option to wrap every property accessor in a try-except to prevent this from happening?
Additional note since I'm sure someone will mention it: I know, a property accessor shouldn't do much, it should just return a private backing field. I agree! But sometimes we have to maintain code that wasn't written with all the best practices in mind.
-
Hi,
Sorry, I don't know. However, I would decorate the problematic properties to keep things in Python and handle the exception there. That way you also keep a clean separation of responsibilities.
-
Hi @SGaist,
Thanks! Is it a bad idea to override the PySide6
@Property
decorator? If I did that, I could put the exception handling right in there and the existing code could remain the same.Thoughts?
-
I was about to suggest adding an explicit decorator to the functions that "misbehave".
I would however not decorate all properties blindly so it can also serve as documentation to avoid getting the same issues in the future.