Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:can';我看不到正在抛出的异常_Python_Exception_Exception Handling - Fatal编程技术网

Python:can';我看不到正在抛出的异常

Python:can';我看不到正在抛出的异常,python,exception,exception-handling,Python,Exception,Exception Handling,我正在运行一个单元测试,我意识到抛出了一个异常。然而,我只是不知道到底是什么被抛出 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'

我正在运行一个单元测试,我意识到抛出了一个异常。然而,我只是不知道到底是什么被抛出

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 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'
运行单元测试时得到的输出是:

setUpClass called
hi1

Process finished with exit code 1
很明显,在:
Widget.\uuuu init\uuuu(self,name,parent)
上出现了一些问题,但我看不出它是什么。有什么方法可以让我打印出抛出的异常或错误吗

编辑:下面是附带的小部件类:

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'
现在它给了我:

setUpClass called
hi1
hey2

Process finished with exit code 1
如您所见,python(2.x)中最常见的异常是:

所以,在您的案例中,通过捕获异常,您将丢失一些其他异常(罕见的异常,但可能发生在您的案例中):SystemExit、KeyboardInterrupt和GeneratorExit。 尝试将except子句更改为:

except BaseException as e:
这样,您将确保捕获所有异常,并检测您的问题

编辑:

但是,PyQT在里面很有趣。如前所述:

在PyQt v5.5中,未经处理的Python异常将导致调用 Qt的qFatal()函数。默认情况下,这将调用abort()和 申请将终止。请注意,安装了一个应用程序 异常钩子仍然优先

这样,一个异常的异常(C++代码中的很多原因,坏的参数……)可以默默地停止你的应用程序。 然而,最后一部分听起来很有用,如果您安装了异常挂钩,它将在静默中止之前被调用。让我们尝试添加一个异常挂钩:

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

我需要在脚本中添加
app=QApplication(sys.argv)
sys.exit(app.exec())
类TestUM(unittest.TestCase):

所以上面的脚本应该如下所示:

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__':
    app = QApplication(sys.argv)
    unittest.main()
    sys.exit(app.exec_())

请注意,这并不能解决我抛出所需异常的问题(因为没有可见的异常)。但它确实解决了问题,脚本将运行。谢谢

我试过了。什么也没变。奇怪的另外,检查编辑。哦,QtWidget init中发生了一些奇怪的事情,它决定停止程序的执行:'(在启动失败的方法之前:)我太困惑了。在widget类之前?或者在带有单元测试的脚本中?也许我们应该去聊天室。
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__':
    app = QApplication(sys.argv)
    unittest.main()
    sys.exit(app.exec_())