0% found this document useful (0 votes)
3 views

python1

The document provides an overview of exception handling in Python, explaining the concept of exceptions and their hierarchy derived from the BaseException class. It details various types of exceptions such as TypeError, ValueError, IndexError, NameError, and KeyError, along with sample programs demonstrating how to handle these exceptions. Additionally, it covers handling multiple exceptions using both lists and tuples, along with the structure of try, except, else, and finally blocks.

Uploaded by

suryatom5775
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

python1

The document provides an overview of exception handling in Python, explaining the concept of exceptions and their hierarchy derived from the BaseException class. It details various types of exceptions such as TypeError, ValueError, IndexError, NameError, and KeyError, along with sample programs demonstrating how to handle these exceptions. Additionally, it covers handling multiple exceptions using both lists and tuples, along with the structure of try, except, else, and finally blocks.

Uploaded by

suryatom5775
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

DEPARTMENT OF INFORMATION TECHNOLOGY

PYTHON PRORAMMING(22IT14303)
FLIPPED CLASSROOM ACTIVITY ON
• EXCEPTION HIERARCHY By
• HANDLING MULTIPLE EXCEPTION VISHAL R S
222071110
B.TECH IT B
What is exception python:

01 02 03
An Exception is an Whenever there is It basically prevents
error that happens an error, Python the program from
during the generates an getting crashed.
execution of a exception that
program. could be handled.
EXPECTION HIERACHY IN
PYTHON:

This means that every


In Python, all exceptions exception in Python is an
are derived from instance
the BaseException class. of BaseException or one
of its subclasses.
The following diagram shows the hierarchy of built-in exception
classes in Python

TYPE INDEX VALUE SYNTAX


ERROR ERROR ERROR ERROR

NAME MEMORY KEY IMPORT


ERROR ERROR ERROR ERROR
INDENTATI SYSTEM ASSERTION
OS ERROR
ON ERROR ERROR ERROR

ZERO
ATTRIBUTE ASSERTION LOGICAL
DIVISION
ERROR ERROR ERROR
ERROR
TYPE ERROR:
• It occurs when a function and
operation are applied in an
incorrect type
Sample program: Output:
try:
66
a=int(input())
b=input()
11
Print(a+b) Type error unsupported
except TypeError as m: oprand type(s)for /:'int'
Print("type error",m) and 'str'
VALUE ERROR:
• It occurs when a function and
operation are applied in an
incorrect value
Sample program: Output:
try:
66
a=int(input())
b=int(input())
hi
Print(a+b) Value error invalid literal
except ValueError as a: for int() with base 10:'hi'
Print("value error",a)
INDEX ERROR:
• It occurs when a function and
operation are applied in an
incorrect index
Sample program: Output:
try:
index error list index out
a=[1,2,3,4]
of range
Print(a[4])
except IndexError as a:
Print("index error",a)
NAME ERROR:
• It occurs when a function and
operation are applied in an
incorrect name
Sample program: Output:
try:
Name error name 'c' is
Print(c)
not defined
except NameError as a:
Print("name error",a)
KEY ERROR:
• It occurs when a function and
operation are applied in an
incorrect key
Sample program: Output:
try: Key error 9
c={1:2,3:3,7:9}
Print(c)
except KeyError as a:
Print("key error",a)
HANDLING MULTIPLE EXCEPTION IN PYTHON :

These are where you define the code that will run when
exceptions occur.

In your code, any raised exceptions trigger the associated


except clause.

Note that where you have multiple except clauses, your


program will run only the first one that triggers and then
ignore the rest.
try:
You do your operation here
SYNTAX except exception:1:
If there is an exception 1,then execute this
FOR block
CAREATIN except exception:2:
If there is an exception 2,then execute this
G block
MULTIPLE except exception:3:
If there is an exception 3,then execute this
EXCEPTIO block

N else:
If there is no exception than execute this
block
finally:
Statement in this block must excute
EXCEPTION HANDLING WITH TRY , EXCEPT,ELSE AND
FINALLY :
TRY:
This block will test the excepted error to occur
EXCEPT:
Here you can handle the error
ELSE:
If there is no exception then this block will be
executed
FINALLY:
Finally block always gets executed either
exception is generated or not
Sample program in multiple exception:
try:
x = int(input("Enter a number: "))
result = 10 / x
except ZeroDivisionError:
print("You cannot divide by zero.")
except ValueError:
print("Invalid input. Please enter a valid
number.")
else:
print("there is an no excetion")
finally:
print("process will be completed")
Catching Multiple Exceptions using a List

• Using a list of exception types you can


catch multiple exceptions in a
single except block in Python.
• This method is similar to using a tuple of
exception types, but it allows you to
easily add or remove exception types as
needed.
SMAPLE EXAMPLE Multiple Exceptions using a List:

# Catch multiple exception using


tuple
try:
# Code that raise Exception
except[TypeError,ValueError]as e:
e:
print("An exception
Catching Multiple Exceptions with Tuple:
• Using a tuple of exception types is a simple
and concise way to catch multiple exceptions
in a single except block in Python.
• When an exception occurs in the try block,
Python checks if the exception is an instance of
any of the exception types listed in the tuple.
• If so, the corresponding except block is
executed.
SMAPLE EXAMPLE Multiple Exceptions using a tuple:

# Catch multiple exception


using tuple
try:
# Code that raise
Exception
except (TypeError, ValueError)
as e:
print("An exception
occurred:", e)

You might also like