When I write code in Python with exception handling I can write code like:
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()
How does this differ from:
try:
some_code_that_can_cause_an_exception()
except:
some_code_to_handle_exceptions()
code_that_needs_to_run_when_there_are_no_exceptions()
In both cases code_that_needs_to_run_when_there_are_no_exceptions() will execute when there are no exceptions. What’s the difference?
Actually, in the second snippet, the last line executes always.
You probably meant
I believe you can use theYou use theelseversion if it makes the code more readable.elsevariant if you don’t want to catch exceptions fromcode_that_needs_to_run_when_there_are_no_exceptions.