擅长:python、mysql、java
<p>再次引发错误,而不是尝试继续。或者简单地让它冒泡到主循环,或者如果您需要更好的错误处理,那么创建您自己的自定义错误。例如:</p>
<pre><code>class MyNotFatalError(Exception):
pass
def do_something():
try:
a, b, c = 1, 2
except ValueError:
raise MyNotFatalError('Something went wrong')
# In your main function
for post in submissions:
try:
do_something()
do_some_other_thing()
do_some_more()
except MyNotFatalError as err:
continue # we could print out the error text here
do_some_last_thing()
</code></pre>
<p>这样可能更好,因为您只捕获您知道要捕获的错误,并且在出现实际错误时仍然让程序崩溃。你知道吗</p>
<p>如果您只是捕获了<code>ValueError</code>,那么它也会截获并隐藏同一类错误的所有其他可能来源。你知道吗</p>