Courses
Courses for Kids
Free study material
Offline Centres
More
Store Icon
Store

NCERT Solutions for Class 12 Computer Science Chapter 1: Exception Handling in Python

ffImage
banner

Stepwise Answers & Exercise Solutions for Exception Handling in Python

Begin your journey with the NCERT Solutions for Class 12 Computer Science Chapter 1 Exception Handling in Python, designed for CBSE 2025–26 students. This page gives you simple, stepwise guidance to every exercise, so you feel confident tackling both easy and challenging questions.


Here, you’ll find clear, exercise-wise answers crafted as per the latest CBSE marking scheme and exam pattern. Whether you need definitions, types of exceptions, or examples, our stepwise Computer Science Class 12 solutions cover everything that matters for your exam preparation.


Download the free NCERT Chapter 1 PDF for quick revision, use revision notes, and avoid common mistakes. Smart preparation using these solutions will help you boost your confidence, improve accuracy, and score better in your board exams.


Stepwise Answers & Exercise Solutions for Exception Handling in Python

Exercises

  1. “Every syntax error is an exception but every exception cannot be a syntax error.” Justify the statement.

    Answer: A syntax error is a specific type of exception that occurs when the rules of Python are not followed while writing the code. However, an exception is a broader term that includes syntax errors as well as other errors like runtime errors and logical errors that arise during the execution phase. Therefore, every syntax error is an exception, but exceptions can also be caused by other reasons and not just syntax errors.


  2. When are the following built-in exceptions raised? Give examples.
    1. ImportError
    2. IOError
    3. NameError
    4. ZeroDivisionError

    Answer:

    • ImportError: Raised when Python cannot find the specified module.
      Example: import module will produce ImportError if 'module' does not exist.
    • IOError: Raised when an input/output operation fails, such as opening a file that does not exist.
      Example: file = open("abc.txt", "r") if 'abc.txt' is missing.
    • NameError: Raised when a variable or function name is not defined.
      Example: print(value), if 'value' was not defined earlier.
    • ZeroDivisionError: Raised when a number is divided by zero.
      Example: print(5/0) will raise this error.
  3. What is the use of a raise statement? Write a code to accept two numbers and display the quotient. Raise an appropriate exception if the denominator is zero (0).

    Answer: The raise statement is used in Python to throw an exception during program execution, intentionally raising an error when required conditions are not met.

    numerator = float(input("Enter the numerator: "))
    denominator = float(input("Enter the denominator: "))
    if denominator == 0:
        raise ZeroDivisionError("Error: Denominator cannot be zero.")
    else:
        quotient = numerator / denominator
        print("Quotient:", quotient)
        
  4. Use assert statement in Question 3 to test the division expression.

    Answer:

    numerator = float(input("Enter the numerator: "))
    denominator = float(input("Enter the denominator: "))
    assert denominator != 0, "Error: Denominator cannot be zero."
    quotient = numerator / denominator
    print("Quotient:", quotient)
        

    If the user enters 0 as denominator, the program raises an AssertionError with the message.
  5. Define the following:
    1. Exception Handling
    2. Throwing an exception
    3. Catching an exception

    Answer:

    • Exception Handling: Writing extra code in programs to display helpful messages or instructions to the user when an exception occurs, ensuring the program doesn't terminate unexpectedly.
    • Throwing an exception: The process where an exception object is created and passed to the runtime system or exception handler when an error occurs.
    • Catching an exception: Executing a suitable handler written to deal with a specific exception when it arises during program execution.
  6. Explain catching exceptions using try and except blocks.

    Answer: In Python, any code that may raise an exception is placed inside a try block. If an exception is raised, the rest of the try block stops executing and the control moves to the associated except block, where handling code is written. This structure allows specific or general exceptions to be handled gracefully without crashing the program.

    try:
        # code that can raise exception
    except ExceptionName:
        # code to handle the exception
        
  7. Consider the code given below and fill in the blanks:
    print (" Learning Exceptions...")
    try:
        num1= int(input ("Enter the first number"))
        num2=int(input("Enter the second number"))
        quotient=(num1/num2)
        print ("Both the numbers entered were correct")
    except _____________: # to enter only integers
        print (" Please enter only numbers")
    except ____________: # Denominator should not be zero
        print(" Number 2 should not be zero")
    else:
        print(" Great .. you are a good programmer")
    ___________: # to be executed at the end
        print(" JOB OVER... GO GET SOME REST")
        

    Answer:

    • First blank: ValueError
    • Second blank: ZeroDivisionError
    • Third blank: finally
    print (" Learning Exceptions...")
    try:
        num1= int(input ("Enter the first number"))
        num2=int(input("Enter the second number"))
        quotient=(num1/num2)
        print ("Both the numbers entered were correct")
    except ValueError: # to enter only integers
        print (" Please enter only numbers")
    except ZeroDivisionError: # Denominator should not be zero
        print(" Number 2 should not be zero")
    else:
        print(" Great .. you are a good programmer")
    finally: # to be executed at the end
        print(" JOB OVER... GO GET SOME REST")
        
  8. Write a code where you use the wrong number of arguments for a math method (e.g., sqrt() or pow()). Use exception handling to catch ValueError.

    Answer:

    import math
    
    try:
        result = math.pow(2, 3, 4, 5)  # pow() expects 2 args
    except TypeError:
        print("TypeError occurred with math.pow()")
    else:
        print("Result:", result)
    
    try:
        result = math.sqrt(9, 2)  # sqrt() expects 1 arg
    except TypeError:
        print("TypeError occurred with math.sqrt()")
    else:
        print("Result:", result)
        
    TypeError will be raised when an incorrect number of arguments are provided, and the exceptions are caught and displayed.
  9. What is the use of finally clause? Use finally clause in the problem in Question 7.

    Answer: The finally clause is used to execute statements regardless of whether an exception occurs or not in the try block. It is often used for resource cleanup.

    print("Learning Exceptions...")
    try:
        num1 = int(input("Enter the first number: "))
        num2 = int(input("Enter the second number: "))
        quotient = (num1 / num2)
        print(quotient)
        print("Both numbers entered were correct")
    except ValueError:
        print("Please enter only numbers")
    except ZeroDivisionError:
        print("Number 2 should not be zero")
    else:
        print("Great.. you are a good programmer")
    finally:
        print("JOB OVER... GO GET SOME REST")
        

Understanding Exception Handling in Python

Understanding exception handling in Python is key for Class 12 Computer Science students. It not only prevents program crashes but also builds logical problem-solving skills, making coding smoother and less stressful in real-world projects.


Practicing with NCERT Solutions Class 12 Computer Science Chapter 1 Exception Handling In Python (2025-26) helps you identify and fix errors efficiently. Use the try-except-finally approach to write robust code and avoid common mistakes often made in exams.


Regular revision of built-in exceptions and practical coding will boost your confidence. Focus on common Python errors to score high marks and gain an edge in your Computer Science board exam preparation.

FAQs on NCERT Solutions for Class 12 Computer Science Chapter 1: Exception Handling in Python

1. What is Exception Handling in Python as per NCERT Class 12 Computer Science Chapter 1?

Exception Handling in Python is a mechanism to manage errors gracefully during program execution, ensuring that the program does not crash unexpectedly. According to CBSE Class 12 Computer Science Chapter 1:

  • It uses try, except, else, and finally blocks.
  • Common exceptions include ZeroDivisionError, ValueError, and TypeError.
  • Structured exception handling improves program reliability and user experience.

2. How should stepwise NCERT answers be written for full marks in Class 12 Computer Science?

To score full marks in Class 12 Computer Science answers, follow these structured steps:

  • Begin with a precise definition or introductory statement.
  • Break down the solution into clear steps according to the CBSE marking scheme.
  • Use bullet points or numbered lists for clarity.
  • Include key terms and highlight important points.
  • For coding questions, add comments and brief explanations for each part.

3. Which types of questions are asked from Exception Handling in Python in CBSE exams?

CBSE exams for Class 12 Computer Science often include:

  • Define and explain exception handling in Python.
  • Write short programs to handle specific exceptions (e.g., ZeroDivisionError).
  • Identify and correct errors in given code snippets.
  • Explain the purpose of each block (try, except, else, finally).
  • State advantages of exception handling with examples.

4. Are definitions and diagrams necessary in NCERT Computer Science answers?

Yes, including definitions is mandatory for conceptual clarity, and diagrams or code snippets are recommended where relevant. This approach:

  • Helps in scoring step marks according to the marking scheme.
  • Makes answers visually appealing and easy to understand.
  • Improves precision and reduces ambiguity in explanations.

5. Where can I download the NCERT Solutions PDF for Class 12 Computer Science Chapter 1?

You can download the free PDF of NCERT Solutions for Class 12 Computer Science Chapter 1 – Exception Handling in Python:

  • Visit trusted educational websites offering exercise-wise stepwise solutions.
  • Look for a single-click PDF download option for offline revision.
  • Ensure the PDF is updated as per the CBSE 2025–26 syllabus.

6. What are the key learning outcomes of Class 12 Computer Science Chapter 1 Exception Handling in Python?

NCERT Solutions for Class 12 Computer Science Chapter 1 help students:

  • Understand types and causes of exceptions in Python.
  • Write programs to handle exceptions using the correct syntax.
  • Apply try-except-else-finally blocks in problem-solving.
  • Explain the importance of robust and maintainable code.

7. How can I avoid common mistakes in answering Exception Handling questions in CBSE exams?

To avoid common mistakes in CBSE Class 12 Computer Science answers:

  • Do not mislabel or omit any part of the try-except-else-finally structure.
  • Always state the type of exception and provide example code.
  • Stick to the syllabus-specific keywords and definitions.
  • Check for syntax errors in code-based answers.

8. What is the CBSE marking scheme for Exception Handling in Python questions?

The CBSE marking scheme for Exception Handling in Python includes:

  • Step marks for definitions, syntax, and code logic.
  • Keywords are crucial – highlight terms like 'try', 'except', 'raise', 'finally'.
  • Partial marks may be awarded for partially correct answers or correct steps, even if the final output is incorrect.

9. Do examiners give partial marks if the steps are correct but the final answer is wrong?

Yes, in CBSE Computer Science exams, partial marks are awarded for correct steps and logical approach, even if the final answer has a slight error. To maximise marks:

  • Show all steps clearly.
  • Use correct keywords and syntax.
  • Attempt every part of the question, as stepwise approach aligns with CBSE evaluation.

10. What are the most important topics to revise from Exception Handling in Python for Class 12 exams?

The key areas to focus on in Class 12 Computer Science Exception Handling in Python are:

  • Definition and syntax of exception handling.
  • Common Python exceptions like ZeroDivisionError, ValueError, IndexError.
  • Application and difference of try, except, else, and finally blocks.
  • Writing short programs to demonstrate exception handling.