Could you give a example of where finally isn’t… pointless, frankly? I mean, we could just leave it out and not indent what was once in the ”finally” code block and save *cough cough* maybe a dozen bytes of disk space. I’m sure there’s something clever here I’m missing, and I’d love to learn what it is. Thanks for another great video!
@DaveGrayTeachesCode Жыл бұрын
Sure! The example I always think of is closing a file no matter what happened before - error or not - other good examples in some answers here: stackoverflow.com/questions/11551996/why-do-we-need-the-finally-clause-in-python
@PeranMe Жыл бұрын
@@DaveGrayTeachesCode aaah, I didn’t consider the case where you exit early! Thanks, much appreciated as always!
@johnaweiss11 ай бұрын
How are these different? try: .... except: .... finally: print("no error") try: .... except: .... print("no error")
@Sedona1196 ай бұрын
finally: will always execute no matter what. If you put quit() or exit() in your code, the finally: would still be executed whereas if you just said print(), the print statement wouldn't run because the code would already be broken. finally: always executes, even after broken code but print() does not.
@johnaweiss6 ай бұрын
@@Sedona119 So the print won't execute if there's a break. So break exits the entre function?