+ 2
Hi, again belal bayrakdar ! In some cases it may be the same, but not in other cases. With finally you are allways garanteed that the code inside finally will be run. Without finally there are no such garantees. Take a example were you use a break or return inside the except or if an exception inside the exception is rised. Then finally give you the opportunity get the last word.
19th Oct 2022, 2:22 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Hi, belal bayrakdar ! try and except belongs together. try without a except cause a syntax error. When you use finally, that included to finally allways will be run. That is not the case without finally. An error stops the process.
19th Oct 2022, 10:47 AM
Per Bratthammar
Per Bratthammar - avatar
0
# Hi, belal bayrakdar ! # No, the loop can be outside the error handler. Here are three examples where finally is usefull. # 1. def test(): try: int("a") except ValueError: print("Value error!") return None finally: print("1: Yippee Ki-Yay!!!\n") test() # 2. i = 9 while i: if True: i -= 1 try: int("a") except ValueError: print("Value error!") break finally: print("2: Yippee Ki-Yay!!!\n") else: print("False!!!") # 3. (Will cause an error.) try: int("a") except ValueError: print("This Value error is caught (but not the next one)!") int("a") finally: print("3: Yippee Ki-Yay!!!")
19th Oct 2022, 4:52 PM
Per Bratthammar
Per Bratthammar - avatar