Skip to content Skip to sidebar Skip to footer

Python: Can't See Exception That Is Getting Thrown

I am running a unit test and I realize that there is an exception getting thrown. However, I am just not sure what exactly is getting thrown. from pt_hil.utilities.PT_HIL_Interface

Solution 1:

As you can see here, the top exceptions in python (2.x) are :

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StandardError
      ....

So, in your case, by catching Exception you are missing some other exceptions (rare exceptions, but probably happening in your case) : SystemExit, KeyboardInterrupt and GeneratorExit. Try to change your except clause into :

except BaseException as e:

this way, you'll be sure to catch all exceptions, and detect your problem.

EDIT:

BUT, PyQT can be fun inside. As mentionned here :

In PyQt v5.5 an unhandled Python exception will result in a call to Qt’s qFatal() function. By default this will call abort() and the application will terminate. Note that an application installed exception hook will still take precedence.

So, an unexcepted exception (that can happend for many reasons in C++ code, bad parameters...) can silently stop your application. Yet, the last part sounds useful, if you install an exception hook, it will be called before the silently abort. Let's try to add a exception hook :

sys._excepthook = sys.excepthook # always save before overriding

def application_exception_hook(exctype, value, traceback):
    # Let's try to write the problemprint"Exctype : %s, value : %s traceback : %s"%(exctype, value, traceback)
    # Call the normal Exception hook after (this will probably abort application)
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

# Do not forget to our exception hook
sys.excepthook = application_exception_hook

Solution 2:

I needed to add app = QApplication(sys.argv) and sys.exit(app.exec_()) in the script with the class TestUM(unittest.TestCase):

so that script above should look like:

from pt_hil.utilities.PT_HIL_Interface_Utils.widgets import PathPicker
import unittest
import wx

classTestUM(unittest.TestCase):

    @classmethoddefsetUpClass(cls):
        print'setUpClass called'
        cls.path_picker = PathPicker()
        print'path_picker has been declared'deftest_PathPicker(self):
        self.assertRaises(NotImplementedError, wx.UIActionSimulator.MouseClick(self.path_picker.browse))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    unittest.main()
    sys.exit(app.exec_())

note that this does not solve the problem I had of throwing the exception I needed (since no exception was visible). But it did solve the problem and the script would run. Thanks!

Post a Comment for "Python: Can't See Exception That Is Getting Thrown"