+ 2

Why do even we have to use Finally ?? we can just write the code outside of "except" block...!

28th Sep 2016, 9:22 AM
Mehdi Hoseini
Mehdi Hoseini - avatar
2 Respostas
+ 5
In Finally-block you write the code that needs to be executed whether your program raises an exception or not. For example, suppose you have a file opened and due to some problem your program terminates. Then your file needs to be closed anyhow. There comes "Finally-block" to the rescue.
8th Oct 2016, 8:35 AM
Harshdeep Singh
Harshdeep Singh - avatar
0
Yes, you can, but the code after the except block will not get executed if an exception is thrown that is not handled by any except block, if the function returns prematurely, or if the try block is in a loop and you jump out via break. Code in the finally block will get executed in these cases, too (= no matter what!).
31st Jan 2017, 7:44 AM
Phipps