🔥 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

Single except block that can handle multiple exceptions in Python

-->We can write a single except block that can handle multiple different types of exceptions.


except (Exception1, Exception2, Exception3, ...):

or

except (Exception1, Exception2, Exception3, ...) as msg:

Parentheses are mandatory and this group of exceptions is internally considered as a tuple.


Example

try:

    x = int(input("Enter First Number: "))

    y = int(input("Enter Second Number: "))

    print(x / y)


except (ZeroDivisionError, ValueError) as msg:

    print("Plz Provide valid numbers only and problem is:", msg)


Output:


D:\Python3>py SingleEx.py

Enter First Number: 10

Enter Second Number: 0

Plz Provide valid numbers only and problem is: division by zero


D:\Python3>py SingleEx.py

Enter First Number: 10

Enter Second Number: ten

Plz Provide valid numbers only and problem is: invalid literal for int() with base 10: 'ten'


No comments:

Post a Comment

Thank you Very Much.For Given Comment

Free 51 Websites for Learning Education and Courses

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