Python: I don’t see the exception that is thrown

Python: I don’t see the exception that is thrown … here is a solution to the problem.

Python: I don’t see the exception that is thrown

I

was running a unit test and I realized that an exception was thrown. However, I’m just not sure exactly what was thrown.

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

class TestUM(unittest. TestCase):

@classmethod
    def setUpClass(cls):
        print 'setUpClass called'
        cls.path_picker = PathPicker()
        print 'path_picker has been declared'

def test_PathPicker(self):
        self.assertRaises(NotImplementedError, wx. UIActionSimulator.MouseClick(self.path_picker.browse))

if __name__ == '__main__':
    unittest.main()

PathPicker class:

class PathPicker(Widget):

def __init__(self, parent=None, name="PathPicker"):
        print 'hi1'
        try:
            Widget.__init__(self, name, parent)
        except Exception as e:
            print 'hello'
            return logging.error(traceback.format_exc())
        print 'hi2'

The output when you run the unit test is:

setUpClass called
hi1

Process finished with exit code 1

Obviously, something went wrong: Widget.__init__ (self, name, parent) But I don’t see what. Is there any way to print out the exception or error that is thrown?

Edit: This is the widget class that goes with it:

class Widget(QWidget):
    def __init__(self, name, parent=None):
        print 'hey2'
        try:
            super(Widget, self).__init__()
        except BaseException as e:
            print 'hello'
            return logging.error(traceback.format_exc())
        print 'hey3'

Now it gives me:

setUpClass called
hi1
hey2

Process finished with exit code 1

Solution

As you can see< a href="https://docs.python.org/2/library/exceptions.html#exception-hierarchy" rel="noreferrer noopener nofollow">here, the most common exception in python (2.x) is:

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

So, in your case,

by catching exceptions you missed some other exceptions (rare exceptions, but can happen in your case): SystemExit, KeyboardInterrupt, and GeneratorExit.
Try changing your except clause to:

except BaseException as e:

This way, you will surely catch all the anomalies and detect your problem.

Edit:

However, PyQT can be interesting inside. As mentioned earlier< a href="http://pyqt.sourceforge.net/Docs/PyQt5/incompatibilities.html#unhandled-python-exceptions" rel="noreferrer noopener nofollow">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.

Therefore, exceptions that are not excluded (which can occur in C++ code for a variety of reasons, wrong parameters…) You can silently stop your application.
However, the last part sounds useful, if you install an exception hook, it will be called before silently aborting. Let’s try adding an exception Hook

:

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

def application_exception_hook(exctype, value, traceback):
    # Let's try to write the problem
    print "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

Related Problems and Solutions