Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Strategy for handling exceptions in property accessors

Strategy for handling exceptions in property accessors

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 2 Posters 224 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    malocascio
    wrote on 21 Mar 2025, 17:04 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 Mar 2025, 19:54 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        malocascio
        wrote on 21 Mar 2025, 20:36 last edited by
        #3

        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?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 22 Mar 2025, 19:40 last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          2/4

          21 Mar 2025, 19:54

          • Login

          • Login or register to search.
          2 out of 4
          • First post
            2/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved