SlideShare a Scribd company logo
EXCEPTIONAL HANDLING
Vinay Agrawal
ASST. PROFESSOR
CSEDEPT.
SHARDA UNIVERSITY, AGRA
Exceptional Handling in the python .pptx
Handling an Exception
Exceptional Handling in the python .pptx
Exceptional Handling in the python .pptx
Exceptional Handling in the python .pptx
Multiple except blocks
try:
statement(s)
except Exception1:
statement
except Exception2:
statement
except Exception3:
statement
Multiple Exceptions in a Single except block
try:
n=int(input(‘Enter the number’))
print(n**2)
except (KeyboardInterrupt, ValueError,TypeError):
print(“Please Check before you enter”)
print(“Bye”)
1. ZeroDivisionError
a=int(input(‘Enter the number’))
b=int(input(‘Enter the number’))
try:
c=a/b
print(c)
except ZeroDivisionError as e:
print(“Please Check Denominator ”,e)
print(“Bye”)
2. ValueError
try:
n=int(input(‘Enter the number’))
print(n**2)
except ValueError as e:
print(“Please Check before you enter ”,e)
print(“Bye”)
3.KeyboardInterrupt
try:
n=int(input(‘Enter the number’))
print(n**2)
except KeyboardInterrupt as e:
print(“Do not press ctrl+c ”,e)
print(“Bye”)
4. TypeError
(1) print(2+’Hello’)
(2) a=5
print(a())
(3) L=[1,2,3,4]
print(L[‘1’])
(4) a=45
for i in a:
print(i)
5. FileNotFoundError
fname=input(“Enter the filename”)
try:
fp=open(fname,’r’)
print(fp.read())
except FileNotFoundError as e:
print(“Please Check before you enter ”,e)
print(“Bye”)
6. PermissionError
fname=input(“Enter the filename”)
try:
fp=open(fname,’w’)
fp.write(‘Vinay’)
fp.close()
except PermissionError as e:
print(“Please Check the Permission of file ”,e)
print(“Bye”)
7. IndexError
L=[1,2,3,4]
try:
print(L[9])
except IndexError as e:
print(“Please Check the index ”,e)
print(“Bye”)
8.NameError
try:
print(a)
except NameError as e:
print(“Please Check the variable a ”,e)
print(“Bye”)
9. KeyError
D={1:1,2:8,3:27,4:64}
try:
print(D[5])
except KeyError as e:
print(“Please Check the key exists or not ”,e)
print(“Bye”)
Raising Exceptions
 We can deliberately raise an exception using the raise keyword
 Syntax
raise [Exception [args]]
Example:
try:
n=10
print(n)
raise ValueError
except:
print(“Exception Occurred”)
Re-raise an Exception
try:
raise NameError
except:
print(“Re-raising the exception”)
raise
Handling Exceptions in Invoked Functions
def fun(a,b):
try:
c=a/b
except ZeroDivisionError:
print(“You can not divide a no. by zero”)
a=int(input(“Enter the Number”))
b=int(input(“Enter the Number”))
fun(a,b)
def fun(a,b):
return a/b
a=int(input(“Enter the Number”))
b=int(input(“Enter the Number”))
try:
fun(a,b)
except ZeroDivisionError:
print(“You can not divide a no bye Zero”)
Programs
1. WAP that prompts the user to enter a number. If the
number is positive or zero, print it, otherwise raise an
exception ValueError with some message.
2. WAP which infinitely print natural numbers. Raise the
StopIteration exception after displaying first 20
numbers to exit from the program.
3. WAP that prompts the user to enter his name. The
program then greets the person with his name. But if
the person’s name is “Rahul”, an exception is thrown
explicitly and he is asked to quit the program.
Tricky Questions (Code 1)
1. What will be the output?
def fun():
try:
print("Hello")
return 1
finally:
print("DONE")
fun()
print("Hi")
2. What will be the output?
def fun():
for i in range(5):
try:
if i==0:
print("Hello")
break
finally:
print("DONE")
fun()
print("Hi")
3. What will be the output?
def fun():
for i in range(5):
try:
if i==0:
print("Hello")
continue
finally:
print("DONE")
fun()
print("Hi")
Exceptional Handling in the python .pptx

More Related Content

DOC
Exception Example in Python
PPTX
Python programming workshop session 3
DOCX
python programs .docx
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PDF
PYTHON_PROGRAM(1-34).pdf
DOCX
C programs
PDF
Week3
DOCX
Mcq cpup
Exception Example in Python
Python programming workshop session 3
python programs .docx
FLOW OF CONTROL-NESTED IFS IN PYTHON
PYTHON_PROGRAM(1-34).pdf
C programs
Week3
Mcq cpup

Similar to Exceptional Handling in the python .pptx (20)

PDF
Python From Scratch (1).pdf
PPTX
Programming Fundamentals in Python - Selection Structure
PPTX
week2.pptx program program program problems
PDF
calculator_new (1).pdf
PPTX
PythonPPT_Raj Programming for 1st year .pptx
PPTX
Kotlin
PPTX
MODULE. .pptx
PPTX
c programming lab for first year student
DOCX
informatics practices practical file
PDF
Astronomical data analysis by python.pdf
DOCX
Programas Gambas
PDF
Class 2: Welcome part 2
PDF
Introduction to python programming
PDF
Kotlin: A pragmatic language by JetBrains
DOCX
Binary addition using class concept in c++
PDF
Some hours of python
PDF
Python Programming
PPTX
Basic python programs
PPTX
Python Conditionals and Functions
Python From Scratch (1).pdf
Programming Fundamentals in Python - Selection Structure
week2.pptx program program program problems
calculator_new (1).pdf
PythonPPT_Raj Programming for 1st year .pptx
Kotlin
MODULE. .pptx
c programming lab for first year student
informatics practices practical file
Astronomical data analysis by python.pdf
Programas Gambas
Class 2: Welcome part 2
Introduction to python programming
Kotlin: A pragmatic language by JetBrains
Binary addition using class concept in c++
Some hours of python
Python Programming
Basic python programs
Python Conditionals and Functions
Ad

More from vinayagrawal71 (8)

PPT
Recursion and Lambda Functions in python.ppt
PPT
Sets in the python programming language.ppt
PPT
File Handling in python programming .ppt
PPTX
Exception Handling in Python programming.pptx
PPT
Dictionarys in python programming language.ppt
PPTX
11Exceptional Handling In Python Language.pptx
PPT
File Handling in Python Programming .ppt
PPTX
Exception Handling in Python Programming.pptx
Recursion and Lambda Functions in python.ppt
Sets in the python programming language.ppt
File Handling in python programming .ppt
Exception Handling in Python programming.pptx
Dictionarys in python programming language.ppt
11Exceptional Handling In Python Language.pptx
File Handling in Python Programming .ppt
Exception Handling in Python Programming.pptx
Ad

Recently uploaded (20)

PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
PPT
Occupational Health and Safety Management System
PPTX
Software Engineering and software moduleing
PDF
Design Guidelines and solutions for Plastics parts
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Artificial Intelligence
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PPTX
introduction to high performance computing
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
Automation-in-Manufacturing-Chapter-Introduction.pdf
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
Exploratory_Data_Analysis_Fundamentals.pdf
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
COURSE DESCRIPTOR OF SURVEYING R24 SYLLABUS
Occupational Health and Safety Management System
Software Engineering and software moduleing
Design Guidelines and solutions for Plastics parts
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Artificial Intelligence
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
"Array and Linked List in Data Structures with Types, Operations, Implementat...
introduction to high performance computing
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf

Exceptional Handling in the python .pptx