Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class 12 - Computer Science - Python Exceptions

Rating
-
Sold
-
Pages
6
Uploaded on
26-08-2025
Written in
2025/2026

Class 12 Computer Science Notes: Exceptions in Python (Comprehensive + Exam-Ready) Overview This PDF is a complete, well-structured guide to the topic Exceptions in Python, designed for Class 12 students as per the CBSE syllabus. The notes explain difficult concepts in simple language, supported with examples and syntaxes. Perfect for both last-minute revision and deep understanding. Contents Covered Introduction to Errors in Python Syntax Errors vs. Runtime Errors vs. Logical Errors Difference between Errors and Exceptions What are Exceptions? Meaning of an Exception Why Exception Handling is needed Built-in Exceptions in Python Common exceptions: ZeroDivisionError, ValueError, IndexError, TypeError, KeyError, NameError Examples with code outputs The try-except Block Syntax of exception handling Single except vs. Multiple except clauses Handling multiple exceptions The else and finally Clauses Purpose of else block in exceptions Using finally for cleanup operations (like closing files, releasing resources) Raising Exceptions Using raise keyword with examples User-defined exception classes Best Practices Writing cleaner error messages Avoiding silent failures Example-based tips for exams Case Study & Solved Programs Program to handle division by zero Program to read a file safely Program to create and raise a user-defined exception Quick Revision Section One-page summary table of syntax, keywords, and examples Important previous year CBSE questions with answers Why These Notes Are Valuable Exam-Oriented → Covers NCERT + CBSE sample papers Time-Saving → Crisp explanation + ready-to-use programs Score Booster → Extra focus on important questions Practical → Includes coding examples with expected output Ideal For: Class 12 CBSE Computer Science students Students preparing for boards, school tests, and viva

Show more Read less
Institution
Course

Content preview

Understanding Errors and Exceptions: Syntax, Semantic,
and Runtime Errors
Errors are an unavoidable part of programming. Python provides tools to identify and manage errors, ensuring that your
programs are reliable and intuitive. This guide focuses on Syntax Errors, Semantic (Logical) Errors, and Runtime Errors,
along with Exception Handling.


Types of Errors in Python
Python errors can be broadly categorized into three types:




Before program
Syntax Error Yes, must fix manually Python parser
execution

Semantic During program Programmer’s
Yes, requires debugging
Error runtime logic

Runtime During program Yes, can be handled with Python
Error runtime try-except interpreter




1. Syntax Errors (Parsing Errors)
What Are They? Syntax errors occur when Python’s grammar rules are violated. For example, missing colons, mismatched
parentheses, or spelling mistakes in keywords.



Detected before the program runs (during parsing).


Python won’t execute the code until these errors are fixed.



Examples of Syntax Errors:


# Missing colon (:) after the loop:
for i in range(5) # SyntaxError: Missing ":"
print(i)


# Mismatched parentheses:
print("Hello World" # SyntaxError: Missing closing parenthesis ")"



What Happens When They Occur?



Python stops execution and displays an error message with:

Error type (SyntaxError)

Description of the violation


Line number and a visual pointer ( ^ ) to where the issue occurred.




Page 1 of 6

, Fix: Carefully read the error message and correct the code to conform to Python’s rules.



2. Semantic Errors (Logical Errors)
What Are They? Semantic errors occur when the code is written correctly (no syntax issues), but the logic or meaning is
incorrect.


Examples of Semantic Errors:


# Example 1: Incorrect formula implementation
radius = 5
area = 2 * 3.14 * radius # Semantic Error: Incorrect formula for area of a circle
print("Area:", area)


# Example 2: Wrong variable used
x = 10
y = 20
result = x * x # Semantic Error: Multiplied x with itself instead of x with y
print(result)



What Happens When They Occur?



Python executes the code without crashing.


Output is unintended or incorrect, which is harder to detect.


Requires testing and debugging to identify the logical flaw.



Fix: Check the logic step-by-step using print statements, debugging tools, or code reviews.



3. Runtime Errors
What Are They? These occur when Python encounters an error while the program is running, disrupting the normal flow.

Common causes of runtime errors:



Dividing by zero.


Accessing out-of-range elements in a list.


Using an undefined variable.


Attempting operations on incompatible data types.



Examples of Runtime Errors:


# Division by zero:
x = 10




Page 2 of 6

Connected book

Written for

Institution
Secondary school
Course
School year
2

Document information

Uploaded on
August 26, 2025
Number of pages
6
Written in
2025/2026
Type
Class notes
Professor(s)
Prasad
Contains
All classes

Subjects

$7.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
shrihari2

Get to know the seller

Seller avatar
shrihari2
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
10 months
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions