когда необходимо добавить предложение `else` в try..except в Python?

Когда я пишу код на 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()

Чем это отличается от:

try:
    some_code_that_can_cause_an_exception()
except:
    some_code_to_handle_exceptions()

code_that_needs_to_run_when_there_are_no_exceptions()

В обоих случаяхcode_that_needs_to_run_when_there_are_no_exceptions() будет выполняться, когда нет исключений. Какие'Разница?

Ответы на вопрос(8)

Ваш ответ на вопрос