I am learning Python and stumbled upon a concept that I cannot easily learn: an optional else block inside a try construct.
According to the documentation :
The try ... except statement has an optional else clause, which, when present, should follow everything except the caveats. This is useful for code that should be executed if the try clause does not raise an exception.
What am I confused about why the code should be executed if the try clause does not throw an exception inside the try construct - why not just execute it after try / except at the same level of indentation? I think this will simplify exception handling options. Or another way to ask if this is code that is in the else block, it won’t do it if it just executed the try statement, regardless of it. Maybe I'm missing something, enlighten me.
This question is somewhat similar to this , but I could not find there what I was looking for.
python exception-handling try-catch
amphibient
source share