检测何时在内部函数中抛出异常并处理异常

时间:2015-08-18 01:16:41

标签: python python-2.7 exception-handling python-sphinx

在使用sphinx进行构建时,我无法弄清楚如何检查何时引发异常。我有以下代码行:

    sphinx.build_main(['-b html', source_dir, target_dir])

如果构建的某些部分不正确,函数build_main main会抛出错误,但是内部的捕获和处理异常意味着我无法明确尝试抓住他们。例如,函数build_main的输出是

Running Sphinx v1.3.1
loading translations [Python]... not available for built-in messages
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 0 source files that are out of date
updating environment: 0 added, 1 changed, 0 removed
reading sources... [100%] test
C:\Users\fakeuser\Documents\Github\travis-sphinx\test\docs\source\test.rst:7: WARNING: autodoc: failed to import module u'test.t
his'; the following exception was raised:
Traceback (most recent call last):
  File "c:\users\grant mercer\anaconda\lib\site-packages\sphinx-1.3.1-py2.7.egg\sphinx\ext\autodoc.py", line 385, in import_object
    __import__(self.modname)
ImportError: No module named this
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] test
generating indices... genindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded, 1 warning.

我故意创建此错误的目的是捕获这样的东西,但我无法弄清楚如何。无论如何都要检测内部函数中何时抛出异常,即使它在我可以使用它之前被捕获并处理了?我试过了

try:
    sphinx.build_main(['-b html', source_dir, target_dir])
except:
    print 'exception!'

但没有抓到(正如预期的那样)

0 个答案:

没有答案