Jan-12-2021, 05:25 PM
Is having code in the "else" block any better than code in the "try" block? Is it because doing so places a harder focus on what code is expected to raise an exception?
Does finally predate context managers? I can see using finally to close a file or do some other cleanup that is not done by other tools. I can see that it eliminates the need for duplicate code in the except and else blocks, but did it have a more important role?
try: dangerous code innocuous code except: handler code #vs try: dangerous code except: handler code else: innocuous codeThe upper example has the benefit of keeping the code together instead of splitting into little pieces. The bottom example has the benefit of pointing out what code is handled by the exception.
Does finally predate context managers? I can see using finally to close a file or do some other cleanup that is not done by other tools. I can see that it eliminates the need for duplicate code in the except and else blocks, but did it have a more important role?