SlideShare a Scribd company logo
Exception Handling in Python
Python
Exceptions
Exception Handling
Try and Except
Nested try Block
Handling Multiple Exceptions in single Except Block
Raising Exception
Finally Block
User Defined Exceptions
Exception
⚫ When writing a program, we, more often than
not, will encounter errors.
⚫ Error caused by not following the proper
structure (syntax) of the language is called syntax
error or parsing error
⚫ Errors can also occur at runtime and these are
called exceptions.
⚫ They occur, for example, when a file we try to
open does not exist (FileNotFoundError), dividing
a number by zero (ZeroDivisionError)
⚫ Whenever these type of runtime error occur, Python
creates an exception object. If not handled properly,
it prints a traceback to that error along with some
details about why that error occurred.
exception handling.pptx
Exception Handling
⚫To handle exceptions, and to call code
when an exception occurs, we can use a
try/except statement.
⚫The try block contains code that might
throw an exception.
⚫If that exception occurs, the code in the
try block stops being executed, and the
code in the except block is executed.
⚫If no error occurs, the code in the except
block doesn't execute.
exception handling.pptx
TypeError
ValueError
NameError
ImportError
IndexError
KeyError
Nested Try
Block
Nested Try
Block
Nested Try Block
exception handling.pptx
exception handling.pptx
⚫Multiple except blocks
⚫A try statement can have
multiple different except blocks
to handle different exceptions.
exception handling.pptx
exception handling.pptx
⚫Multiple exceptions in a single
except block
⚫Multiple exceptions can also be put into a
single except block using parentheses, to
have the except block handle all of
them.
exception handling.pptx
exception handling.pptx
Raising Exceptions
Raising Exception
⚫Raising exception is similar to
throwing exception in C++/Java.
⚫You can raise exceptions by
using the raise statement
User Defined Exception
finally
⚫To ensure some code runs no matter
what errors occur, you can use a
finally statement.
⚫The finally statement is placed at the
bottom of a try/except statement.
⚫Code within a finally statement
always runs after execution of the
code in the try, and possibly in the
except, blocks.
exception handling.pptx
exception handling.pptx
⚫Code in a finally statement even runs
if an uncaught exception occurs in one
of the preceding blocks.
USER DEFINED EXCEPTIONS
USER DEFINED EXCEPTIONS
USER DEFINED EXCEPTIONS
USER DEFINED EXCEPTIONS
PROGRAM TO COUNT THE TOTAL NUMBER OF WORDS IN
A FILE
PROGRAM TO COPY THE CONTENT OF ONE FILE INTO
ANOTHER
f=open("swap.py","r")
f1=open("birds.py","w")
s=f.read()
print(s)
for i in s:
f1.write(i)
exception handling.pptx
exception handling.pptx
MODULES
MODULES
MODULES
MODULES
MODULES
PACKAGES
PACKAGES
PACKAGES
PACKAGES
PACKAGES
PACKAGES
PACKAGES
PACKAGES

More Related Content

Similar to exception handling.pptx (20)

PPTX
Exception handling with python class 12.pptx
PreeTVithule1
 
PPT
Exception handling in python and how to handle it
s6901412
 
PPT
Exception Handling using Python Libraries
mmvrm
 
PPTX
ACP - Week - 9.pptx
funnyvideosbysam
 
PPTX
Exception handling.pptxnn h
sabarivelan111007
 
PPTX
exceptioninpython.pptx
SulekhJangra
 
PDF
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
PPT
Exception
Navaneethan Naveen
 
PPTX
EXCEPTION HANDLING IN PYTHON For students .py.pptx
MihirBhardwaj3
 
PPT
Py-Slides-9.ppt
wulanpermatasari27
 
PPTX
Python Lecture 7
Inzamam Baig
 
PPTX
Exception Handling in Python Programming.pptx
vinayagrawal71
 
PDF
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
PPTX
Exception Handling in Python topic .pptx
mitu4846t
 
PPTX
Python Exception Handling
Megha V
 
PPTX
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
 
PPTX
Python for beginners textbook slides ppt
AnithaChristyAngelin
 
PDF
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
 
PDF
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Exception handling with python class 12.pptx
PreeTVithule1
 
Exception handling in python and how to handle it
s6901412
 
Exception Handling using Python Libraries
mmvrm
 
ACP - Week - 9.pptx
funnyvideosbysam
 
Exception handling.pptxnn h
sabarivelan111007
 
exceptioninpython.pptx
SulekhJangra
 
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
EXCEPTION HANDLING IN PYTHON For students .py.pptx
MihirBhardwaj3
 
Py-Slides-9.ppt
wulanpermatasari27
 
Python Lecture 7
Inzamam Baig
 
Exception Handling in Python Programming.pptx
vinayagrawal71
 
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
Exception Handling in Python topic .pptx
mitu4846t
 
Python Exception Handling
Megha V
 
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
 
Python for beginners textbook slides ppt
AnithaChristyAngelin
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Edureka!
 
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 

Recently uploaded (20)

PDF
TESDA License NC II PC Operations TESDA, Office Productivity
MELJUN CORTES
 
PDF
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
PDF
SaleServicereport and SaleServicereport
2251330007
 
PDF
Loading Data into Snowflake (Bulk & Stream)
Accentfuture
 
PPTX
covid 19 data analysis updates in our municipality
RhuAyungon1
 
PDF
Group 5_RMB Final Project on circular economy
pgban24anmola
 
PPTX
BinarySearchTree in datastructures in detail
kichokuttu
 
PDF
5991-5857_Agilent_MS_Theory_EN (1).pdf. pdf
NohaSalah45
 
PDF
Unlocking Insights: Introducing i-Metrics Asia-Pacific Corporation and Strate...
Janette Toral
 
PPTX
Module-2_3-1eentzyssssssssssssssssssssss.pptx
ShahidHussain66691
 
PPTX
Generative AI Boost Data Governance and Quality- Tejasvi Addagada
Tejasvi Addagada
 
PDF
Business implication of Artificial Intelligence.pdf
VishalChugh12
 
PPTX
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
PDF
Technical-Report-GPS_GIS_RS-for-MSF-finalv2.pdf
KPycho
 
DOCX
INDUSTRIAL BENEFIT FROM MICROSOFT AZURE.docx
writercontent500
 
PPTX
在线购买英国本科毕业证苏格兰皇家音乐学院水印成绩单RSAMD学费发票
Taqyea
 
PPTX
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
DOCX
🧩 1. Solvent R-WPS Office work scientific
NohaSalah45
 
PPTX
Artificial intelligence Presentation1.pptx
SaritaMahajan5
 
PPTX
04_Tamás Marton_Intuitech .pptx_AI_Barometer_2025
FinTech Belgium
 
TESDA License NC II PC Operations TESDA, Office Productivity
MELJUN CORTES
 
A Web Repository System for Data Mining in Drug Discovery
IJDKP
 
SaleServicereport and SaleServicereport
2251330007
 
Loading Data into Snowflake (Bulk & Stream)
Accentfuture
 
covid 19 data analysis updates in our municipality
RhuAyungon1
 
Group 5_RMB Final Project on circular economy
pgban24anmola
 
BinarySearchTree in datastructures in detail
kichokuttu
 
5991-5857_Agilent_MS_Theory_EN (1).pdf. pdf
NohaSalah45
 
Unlocking Insights: Introducing i-Metrics Asia-Pacific Corporation and Strate...
Janette Toral
 
Module-2_3-1eentzyssssssssssssssssssssss.pptx
ShahidHussain66691
 
Generative AI Boost Data Governance and Quality- Tejasvi Addagada
Tejasvi Addagada
 
Business implication of Artificial Intelligence.pdf
VishalChugh12
 
Feb 2021 Ransomware Recovery presentation.pptx
enginsayin1
 
Technical-Report-GPS_GIS_RS-for-MSF-finalv2.pdf
KPycho
 
INDUSTRIAL BENEFIT FROM MICROSOFT AZURE.docx
writercontent500
 
在线购买英国本科毕业证苏格兰皇家音乐学院水印成绩单RSAMD学费发票
Taqyea
 
办理学历认证InformaticsLetter新加坡英华美学院毕业证书,Informatics成绩单
Taqyea
 
🧩 1. Solvent R-WPS Office work scientific
NohaSalah45
 
Artificial intelligence Presentation1.pptx
SaritaMahajan5
 
04_Tamás Marton_Intuitech .pptx_AI_Barometer_2025
FinTech Belgium
 
Ad

exception handling.pptx