Computer ScienceClass 12h apter

Understanding h apter in Class 12 Computer Science NCERT

By ConceptScroll Team · Published on 2 July 2026 · 4 min read

In Class 12 NCERT Computer Science, the h apter topic covers raising exceptions in Python. This concept helps you handle errors effectively by deliberately triggering exceptions during program execution to maintain control flow.

What Is Raising Exceptions in Python?

Raising exceptions is a fundamental concept in Python programming covered in the Class 12 NCERT Computer Science syllabus. It allows programmers to manually trigger errors when certain conditions are met, interrupting the normal flow of the program.

  • Automatic Exceptions: Python interpreter raises exceptions automatically when errors occur, like division by zero.
  • Manual Exceptions: Using the raise statement, you can explicitly throw an exception with a custom message.

Syntax of the raise statement:

``python raise ExceptionName("Error message") ``

For example, raising a ValueError with a message:

``python raise ValueError("Invalid input: must be positive") ``

Raising exceptions helps you control error handling proactively, an important skill for Class 12 students preparing for exams.

Using the Assert Statement to Validate Conditions

The assert statement is another way to raise exceptions in Python, mainly used to check if a condition is true. If the condition is false, it raises an AssertionError with an optional message.

Syntax:

``python assert condition, "Error message" ``

Example: Check if a number is non-negative:

```python def check_positive(num): assert num >= 0, "Number must be non-negative" print("Valid number")

check_positive(5) # Valid number check_positive(-3) # Raises AssertionError ```

This method is useful for validating inputs or preconditions in functions, ensuring your program runs smoothly and safely.

Want to test yourself on h apter? Try our free quiz →

Common Built-in Exceptions and When They Occur

Understanding built-in exceptions is crucial for Class 12 students to debug and write error-free code. Here are some common exceptions:

Exception NameWhen It OccursExample Code Snippet
IndexErrorAccessing invalid index in a list or tuplelst = [1,2]; print(lst[5])
ZeroDivisionErrorDividing by zeroprint(10/0)
ImportErrorFailing to import a moduleimport non_existent_module
IOErrorIssues with file input/outputopen('missing.txt')
NameErrorUsing an undefined variableprint(undefined_var)

Knowing these helps you anticipate errors and handle them using try-except blocks effectively.

How to Use Raise Statement with Examples

The raise statement is used to throw exceptions intentionally. Here is a practical example from Class 12 NCERT Computer Science:

Example: Accept two numbers and display their quotient. Raise an exception if the denominator is zero.

``python try: num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) if num2 == 0: raise ZeroDivisionError("Denominator cannot be zero") quotient = num1 / num2 print("Quotient is", quotient) except ZeroDivisionError as e: print("Error:", e) except ValueError: print("Please enter valid numbers") ``

This program demonstrates how raising an exception helps prevent runtime errors and informs the user about invalid input.

Difference Between Raise and Assert Statements

Understanding the difference between raise and assert is important for exam clarity:

Featureraise Statementassert Statement
PurposeManually throw any exceptionCheck a condition and raise AssertionError if false
UsageUsed to signal errors explicitlyUsed mainly for debugging and validating assumptions
Syntaxraise ExceptionName("message")assert condition, "message"
When Exception RaisedImmediately when raise is executedOnly if condition evaluates to False
Typical Use CaseEnforce constraints, handle errorsDebugging, input validation

Both are part of the Class 12 NCERT syllabus and help maintain robust code.

Next Steps: Handling Exceptions Gracefully

After learning how to raise exceptions, the next important topic is handling them using try-except blocks. Handling exceptions prevents program crashes and allows you to provide meaningful error messages.

Basic syntax:

``python try: # Code that may raise exception except ExceptionType: # Code to handle exception ``

Example:

``python try: x = int(input("Enter a number: ")) y = 10 / x print(y) except ZeroDivisionError: print("Cannot divide by zero") except ValueError: print("Invalid input") ``

Mastering raising and handling exceptions is essential for Class 12 students to write error-resilient programs as per NCERT guidelines.

Frequently asked questions

What is the purpose of the raise statement in Python?

The raise statement explicitly throws an exception to signal an error or special condition.

How does assert differ from raise in exception handling?

Assert checks a condition and raises AssertionError if false, while raise manually throws any exception.

When is IndexError raised in Python?

IndexError occurs when accessing an invalid index in a list or tuple.

Can every exception be a syntax error?

No, every syntax error is an exception, but not every exception is a syntax error.

How to raise an exception if denominator is zero in division?

Use raise ZeroDivisionError("Denominator cannot be zero") before dividing.

Ready to ace this chapter?

Get the full h apter chapter — interactive notes, diagrams, worked solutions, polls and a free practice quiz — in the ConceptScroll app.

Open in ConceptScroll →

Study smarter with ConceptScroll

Daily NCERT-aligned reels, AI doubt solving and chapter quizzes — all free.

Start learning free
#assert statement#cbse#class 12#computer science#exception handling#h apter#ncert#programming#python#raise exception

Continue reading