相关疑难解决方法(0)

Python try-else

声明的可选else条款的用途是try什么?

python exception-handling

532
推荐指数
11
解决办法
29万
查看次数

什么时候需要在Python中的try..except中添加`else`子句?

当我使用异常处理在Python中编写代码时,我可以编写如下代码:

try:
    some_code_that_can_cause_an_exception()
except:
    some_code_to_handle_exceptions()
else:
    code_that_needs_to_run_when_there_are_no_exceptions()
Run Code Online (Sandbox Code Playgroud)

这有什么不同于:

try:
    some_code_that_can_cause_an_exception()
except:
    some_code_to_handle_exceptions()

code_that_needs_to_run_when_there_are_no_exceptions()
Run Code Online (Sandbox Code Playgroud)

在这两种情况下code_that_needs_to_run_when_there_are_no_exceptions()都会在没有例外时执行.有什么不同?

python exception

4
推荐指数
3
解决办法
537
查看次数

标签 统计

python ×2

exception ×1

exception-handling ×1