🔥 Welcome to my blog! Latest updates available now. Visit daily! 🔥 🔥 Click Here for Latest Updates – Visit My Blog 🔥
🔥 Latest Updates 📊 GP Grade Finder 📢 Government Services Info 🆕 New Schemes Updates

Different Cases in try,except and finally blocks in Python

Case-1: If there is no exception

1) try:

2)     print("try")

3) except:

4)     print("except")

5) finally:

6)     print("finally")


Output

try

finally


Case-2: If there is an exception raised but handled


1) try:

2)     print("try")

3)     print(10/0)

4) except ZeroDivisionError:

5)     print("except")

6) finally:

7)     print("finally")


Output

try

except

finally


Case–3: If there is an exception raised but not handled:

1) try:

2)     print("try")

3)     print(10/0)

4) except NameError:

5)     print("except")

6) finally:

7)     print("finally")


Output:o

try

finally

ZeroDivisionError: division by zero (Abnormal Termination)


✅Note:There is only one situation where the finally block won’t be executed, i.e., whenever we are using the os._exit(0) function.


==>Whenever we use the os._exit(0) function, the Python Virtual Machine itself is shut down.

==>In this particular case, the finally block won’t be executed.


1) import os

2) try:

3)     print("try")

4)     os._exit(0)

5) except NameError:

6)     print("except")

7) finally:

8)     print("finally")


Output:

try

✅Note:os._exit(0)

0 represents the status code

It indicates normal termination

Multiple status codes are possible.

No comments:

Post a Comment

Thank you Very Much.For Given Comment

Free 51 Websites for Learning Education and Courses

🛑 లక్à°·à°²ు à°–à°°్à°šుà°ªెà°Ÿ్à°Ÿి à°•ాà°²ేà°œీà°²్à°²ో à°¨ేà°°్à°šుà°•ోà°²ేà°¨ి à°µిà°·à°¯ాలను, à°«్à°°ీà°—ా à°¨ేà°°్à°ªింà°šే 51 à°…à°¦్à°­ుతమైà°¨ à°µెà°¬్‌à°¸ైà°Ÿ్à°²ు ఇవే! 🛑 à°Žà°¡్à°¯ుà°•ేà°·à°¨్ and à°•ోà°°్à°¸ుà°²ు: Co...