When I write code inPython 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_n...
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
try:
some_code_that_can_cause_an_exception()
code_that_needs_to_run_when_there_are_no_exceptions()
except:
some_code_to_h