擅长:python、mysql、java
<p>一般来说,当您只是引发另一个异常时,我认为不需要添加try/except块。通常,您引发的异常与您调用的任何对象引发的异常基本相同(因为问题仍然是相同的)。我所能找到的任何官方文档(除了“<a href="https://www.python.org/dev/peps/pep-0020/" rel="nofollow">[s]imple is better than complex</a>”)都不支持这一点;我能找到的最好的文档是<a href="https://docs.python.org/2/tutorial/errors.html#user-defined-exceptions" rel="nofollow">the list of built-in exceptions</a>,上面写着:</p>
<blockquote>
<p>When creating a module that can raise several distinct errors, a common practice is to create a base class for exceptions defined by that module, and subclass that to create specific exception classes for different error conditions</p>
</blockquote>
<p>这似乎只是解释了如何创建自定义异常,而不是何时创建。你知道吗</p>