'else' runs when for, while loop finished without being interrupted.
To qoute Luciano Ramalho from his book 'Fluent Python',:
I think else is a very poor choice for the keywords in all cases except if. It implies an excluding alternative, like "Run this loop, otherwise to that," but the semantics for else in loops is the opposite: "Run this loop, then do that." This suggests then as a better keyword - which would also make sens in the try context: "Try this, then do that."
Done of nitpicking, why we use try-except-else then? EAFP style coding - at least I believe so.
EAFP - Easier ask Forgiveness Than Permission. Try something first, if error, then do else. This clearly shows coder's intention to catch cases.
This also results better performance if there's few things to trigger error. For example, let's see this extreme case:
import timeit
import random
source = [i for i in range(600000)]
source[random.randint(0, len(source))] = 'bam!'
def EAFP():
numbers = []
for n in source:
try:
result = n + 3
except TypeError:
pass
else:
numbers.append(result)
def LBYL():
numbers = []
for n in source:
if isinstance(n, int):
result = n + 3
numbers.append(result)
print(f"{timeit.timeit(EAFP, number=10):.3f}")
print(f"{timeit.timeit(LBYL, number=10):.3f}")
Results(sec):
2.824
3.393
This would build up if there's more if statements to filter out more possible errors.
And, on readability aspect, lets quote 'Zen of python':
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Multiple nested if is complex than flat try - except - except... - else chain.