Can we remove finally: ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can we remove finally: ?

What is the difference between these codes : Code 1: try: x except: y finally: z Code 2: try: x except: y z

20th Jun 2020, 5:06 PM
Mr.Lightning
Mr.Lightning - avatar
2 Answers
+ 4
You can remove finally if your application does not need to perform any operation if exception raised while handling exception as shown below. try: raise ValueError("code1") except ValueError as e: print("code 1 except") raise ValueError("code11") finally: print('code 1 finally' ) Code under finally block will be executed even if the exception raised while handling exception. Run above code and check with and without finally block.
20th Jun 2020, 6:13 PM
$ยข๐Žโ‚น๐”ญ!๐จ๐“
$ยข๐Žโ‚น๐”ญ!๐จ๐“ - avatar
20th Jun 2020, 5:26 PM
Mr.Lightning
Mr.Lightning - avatar