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'


How to print exception information in Python

try:

    print(10/0)

except ZeroDivisionError as msg:

    print("exception raised and its description is:", msg)

Output:

exception raised and its description is: division by zero


try with multiple except blocks:


👉The way of handling exception is varied from exception to exception.

👉Hence,we have to provide separate except block for every exception type.

👉That is, try with multiple except blocks is possible and recommended to use.


Example:

try:

    ------

    ------

    ------

except ZeroDivisionError:

    perform alternative arithmetic operations

except FileNotFoundError:

    use local file instead of remote file


👉If try with multiple except blocks available, then based on the raised exception, the corresponding except block will be executed.


Example: Multiple except blocks in Python


try:

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

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

    print(x / y)


except ZeroDivisionError:

    print("Can't Divide with Zero")


except ValueError:

    print("please provide int value only")

Program Execution:



D:\Python3>py Multi.py

Enter First Number: 10

Enter Second Number: 5

2.0


D:\Python3>py Multi.py

Enter First Number: 10

Enter Second Number: 0

Can't Divide with Zero


D:\Python3>py Multi.py

Enter First Number: 10

Enter Second Number: five

please provide int value only


✅Note: If try has multiple except blocks, the order of the except blocks is important.

👉The Python interpreter always checks from top to bottom and executes the first matching except block.

Example 

1) try:

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

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

4) print(x / y)

5) except ArithmeticError:

6) print("ArithmeticError")

7) except ZeroDivisionError:

8) print("ZeroDivisionError")


D:\Python3> py MulEx.py


Enter First Number: 5

Enter Second Number: 0

ArithmeticError


✅Note:

Since ZeroDivisionError is a subclass of ArithmeticError, the except ArithmeticError block will catch it first. That’s why exception order matters in Python.

Control Flow in try–except Python

 try:

    stmt-1

    stmt-2

    stmt-3

except XXX:

    stmt-4

stmt-5

Case–1:

If there is no exception

1, 2, 3, 5 → Normal Termination

Case–2:

If an exception is raised at stmt-2 and the corresponding except block is matched

1, 4, 5 → Normal Termination

Case–3:

If an exception is raised at stmt-2 and the corresponding except block is not matched

1 → Abnormal Termination

Case–4:

If an exception is raised at stmt-4 or at stmt-5, then it is always abnormal termination.


✅Conclusions:

👉Within the try block, if an exception is raised anywhere, then the rest of the try block will not be executed, even though we handled that exception.


👉Hence, we have to take only risky code inside the try block, and the length of the try block should be as small as possible.


👉In addition to the try block, there may be a chance of raising exceptions inside except and finally blocks also.


👉If any statement which is not part of the try block raises an exception, then it is always abnormal termination.

Customized Exception Handling by using try-except in Python

👉It is highly recommended to handle

     exceptions.

👉The code which may raise an exception is

     called risky code, and we have to take      

     risky code inside the try block.

👉The corresponding handling code we

      have to take inside the except block.


Syntax:

try:

    Risky Code

except XXX:

    Handling code / Alternative Code


Example:without try-except:


1. print("stmt-1")

2. print(10/0)

3. print("stmt-3")


Output:

stmt-1

ZeroDivisionError: division by zero

Abnormal termination / Non-Graceful Termination


Example:with try-except:

1. print("stmt-1")

2. try:

3. print(10/0)

4. except ZeroDivisionError:

5. print(10/2)

6. print("stmt-3")


Output:

stmt-1

5.0

stmt-3

Normal termination / Graceful Termination

Python Exception Hierarchy



👉Every Exception in Python is a class.

👉All exception classes are child classes

     of BaseException, i.e., every exception

     class extends BaseException either

     directly or indirectly.

👉Hence, BaseException acts as the root

      for the Python Exception Hierarchy.

👉Most of the time, being a programmer, 

     we have to concentrate on Exception

     and its child classes.

Customized Exception Handling by using  try-except

👉It is highly recommended to handle

     exceptions.

👉The code which may raise an exception is

     called risky code, and we have to take      

     risky code inside the try block.

👉The corresponding handling code we

      have to take inside the except block.

Default Exception Handling in Python



👉Every exception in Python is an object. 

👉For every exception type, the 

     corresponding classes are available.

👉Whenever an exception occurs, PVM will create the corresponding exception object and will check for handling code. 

👉If handling code is not available, then the Python interpreter terminates the program abnormally and prints corresponding exception information to the console.

👉The rest of the program won’t be executed.

Ex:Python1.py

1) print("Hello")

2) print(10/0)

3) print("Hi")

Output:


D:\Python3>py Python1.py

Hello

Traceback (most recent call last):

  File "test.py", line 2, in <module>

    print(10/0)

ZeroDivisionError: division by zero

What is Exception in Python?



👉An unwanted and unexpected event 

     that disturbs normal flow of program

     is called exception.

Ex:

ZeroDivisionError

TypeError

ValueError

FileNotFoundError

EOFError

SleepingError

TyrePuncturedError


👉It is highly recommended to handle 

     exceptions.The main objective of 

     exception handling is Graceful 

     Termination of the program.


👉Exception handling does not mean  

     repairing exception. We have to define

    alternative way to continue rest of the

    program normally.

Ex:

👉For example our programming 

     requirement is reading data from remote

     file locating at London. At runtime if 

     London file is not available then the

     program should not be terminated 

     abnormally.

    We have to provide local file to continue

    rest of the program normally.

    This way of defining alternative is nothing

     but exception handling.


try:

Copy code


read data from remote file locating at london

exception FileNotFoundError:

    use local file and continue rest of the

    program normally

Questions

What is an Exception?

What is the purpose of Exception Handling?

What is the meaning of Exception Handling?

Runtime Errors in Python

 


👉Also known as exceptions.

While executing the program if something goes wrong because of end user input or programming logic or memory problems,then we will get Runtime Errors.


Ex: print(10/0)

==>Zero DivisionError: division by zero


print(10/"ten") 

==>TypeError: unsupported operand type(s) for /: 'int' and 'str'


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

print(x)


D:\Python3>py Excep.py

Enter Number:ten


ValueError: invalid literal for int() with base 10: 'ten'


✅Note: Exception Handling concept applicable for Runtime Errors but not for syntax errors.

Syntax Errors in Python



👉In any programming language there are two types of errors.

1. Syntax Errors

2. Runtime Errors


1. Syntax Errors:

👉The errors which occurs because of invalid syntax are called syntax errors.

Ex: x=10

      if x==10

      print("Hello")

      SyntaxError: invalid syntax

Ex:

      print "Hello"

      SyntaxError: Missing parentheses in  

       call to 'print' function.

✅Note:

     Programmer is responsible to correct

     these syntax errors.Once all syntax errors 

     corrected then only program execution

     will be started.

Unified Family Survey Dec-2025

👉 Check Your UFS SURVEY REPORT STATUS


Objectives of Unified Family Survey


1️⃣Collection of data required by departments for policymaking*


*2️⃣Enabling proactive delivery of benefits & services (Category B to Category A)*


*3️⃣Improving saturation and quality of data in the RTGS data lake*

Unified Family Survey – Top 10 Important Points (Very Short Version)

 ✅ Top 10 Important Points

1. 100% e-KYC తప్పనిసరి – à°ª్à°°à°¤ి à°µ్యక్à°¤ి Aadhaar à°¦్à°µాà°°ా à°µెà°°ిà°«ై à°šేà°¯ాà°²ి.

2. Mobile App à°¦్à°µాà°°ా à°®ాà°¤్à°°à°®ే సర్à°µే-- à°…à°¨్à°¨ి à°¡ేà°Ÿా à°¡ిà°œిà°Ÿà°²్‌à°—ా నమోà°¦ు.

3. Individual + Family Level Data-- à°°ెంà°¡ూ తప్పనిసరిà°—ా à°¸ేà°•à°°ింà°šాà°²ి.

4. Name, Gender, DOB, Aadhaar--à°Žà°•్à°•ువగా ఆటో-à°ªాà°ª్à°¯ుà°²ేà°Ÿ్ à°…à°µుà°¤ాà°¯ి.

5. Mobile Number – à°’à°• à°¨ంబర్ à°’à°• à°µ్యక్à°¤ిà°•ి à°®ాà°¤్à°°à°®ే, OTP à°¦్à°µాà°°ా à°µెà°°ిà°«ై.

6. Address (Current + Permanent)-document proofs à°¤ో à°šెà°•్ à°šేà°¯ాà°²ి.

7. Education, Skills, Occupation, Income– self-report + documents (where available).

8. House Details – Water, LPG, Electricity, Toilet, Roof Type à°µంà°Ÿిà°µి surveyor verify.

9. Household Assets– Vehicles, Electronics, Farm Machinery, Animals à°®ొదలైనవి నమోà°¦ు.

10. Survey Timeline – 15 Dec to 12 Jan; à°°ోà°œూ à°®ాà°¨ిà°Ÿà°°ింà°—్ à°‰ంà°Ÿుంà°¦ి.



✅Unified Family Survey App Download Clock Here