0% found this document useful (0 votes)
4 views14 pages

Python Exception Handling Guide

Uploaded by

V S SHRUTHI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Python Exception Handling Guide

Uploaded by

V S SHRUTHI
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Exception Handling in Python

An Interactive Learning Experience -


Grade XII CBSE
Introduction to Exceptions
• Exceptions are errors detected during
execution.
• They disrupt the normal flow of a program.
• Example: Dividing a number by zero.
Common Exceptions in Python
• • ZeroDivisionError
• • ValueError
• • TypeError
• • IndexError
• Each exception type signals a different kind of
error.
Try–Except Block
• try:
• # code that might raise an error
• except ExceptionType:
• # code to handle the error
Else and Finally Clauses
• • else: Executes if no exception occurs
• • finally: Executes no matter what
Raising Exceptions
• Use the raise statement to throw an exception
manually.
• Example:
• raise ValueError("Invalid input")
Creating Custom Exceptions
• class MyError(Exception):
• pass
• raise MyError("Something went wrong")
Interactive Quiz (MCQs)
• Q1. What type of error occurs when you
divide by zero?
• A. TypeError
• B. ValueError
• C. ZeroDivisionError
• (D. SyntaxError)
Matching Errors Activity
• Match the exception with the cause:
• 1. IndexError - Accessing out-of-range index
• 2. TypeError - Invalid data type operation
Debug the Code
• Guess the error in the code:

• x = 10
• y=0
• print(x/y)
Embedded Video
• Watch this video to learn more about
exception handling:
• [Link]
v=NIWwJbo-9_8
Hands-On Task
• Write a Python program that handles:
• • ZeroDivisionError
• • ValueError
• • Custom Exception
Summary
• • Exceptions help manage errors gracefully
• • Use try–except blocks
• • Include else and finally when needed
• • Create custom exceptions
Exit Quiz
• Q. What does the finally block do?
• A. Executes only if there is an error
• B. Skips if try block works
• (C. Executes no matter what)
• D. None of the above

You might also like