Question on basic python

In try and except and else block when the else block will be executed?


Thanks for reaching out on community! Looks like I will need a bit more context here. Can you expand on this one?


Hi @Mukesh,

Great question! I found this in the core Python docs on the try statement:

The optional else clause is executed if the control flow leaves the try suite, no exception was raised, and no return, continue, or break statement was executed. Exceptions in the else clause are not handled by the preceding except clauses.

In other words, the else clause gets executed:

  • Exception caught: no
  • Exception not caught: no
  • No exception raised at all: yes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.