SlideShare a Scribd company logo
1 of 14
PYTHON
AGENDA
Exception
Handling an exception
What is Exception?
 An exception is an event, which occurs during
the execution of a program, that disrupts the normal flow
of the program's instructions.
 In general, when a Python script encounters a
situation that it can't cope with, it raises an exception..
 When a Python script raises an exception, it
must either handle the exception immediately
otherwise it would terminate and come out.
Exceptions Types
EXCEPTION NAME DESCRIPTION
ZeroDivisonError Raised when division or modulo by zero
takes place for all numeric types.
ImportError Raised when an import statement fails.
IOError Raised when an input/ output operation
fails, the open() function when trying to
open a file that does not exist.
SyntaxError Raised when there is an error in Python
syntax.
IndentationError Raised when indentation is not specified
properly.
Handling an exception
If any suspicious code that may raise an exception
suspicious code in a try: block. After the try: block,
include an except: statement, followed by a block of
code
which handles the problem as elegantly as possible.
• SYNTAX:
Here is simple syntax of try....except...else blocks:
try:
You do your operations here;
......................
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
......................
else:
If there is no exception then execute this block.
 A single try statement can have multiple except statements.
This is useful when the try block contains statements that may
throw different types of exceptions.
 Also provide a generic except clause, which handles any
exception.
 After the except clause(s), you can include an else-clause. The
code in the else-block executes if the code in the try: block does
not raise an exception.
• Example
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
except IOError:
print "Error: can't find file or read data"
else:
print "Written content in the file successfully"
fh.close()
Output:
Written content in the file successfully
• The except clause with multiple exceptions:
try:
You do your operations here;
......................
except(Exception1, Exception2,...ExceptionN):
If there is any exception from the given exception list,
then execute this block.
......................
else:
If there is no exception then execute this block.
• The try-finally clause:
The finally block is a place to put any code that must
execute, whether the try-block raised an exception or not
try:
fh = open("testfile", "w")
fh.write("This is my test file for exception handling!!")
finally:
print "Error: can't find file or read data"
If you do not have permission to open the file in writing mode,
Output:
Error: can't find file or read data
• Raising an exceptions:
Raise an exceptions in several ways by using the raise
statement.
SYNTAX:
raise Exception [ args ]
Here, Exception is the type of exception (for example,
NameError) and argument is a value for the exception
argument. The argument is optional; if not supplied, the
exception argument is None.
• Example:
level = 1
if level < 2:
raise "Invalid level!", level
# The code below to this would not be executed
# if we raise the exception
• In order to catch an exception, an "except" clause must refer to
the same exception thrown either class object or simple string.
For example, to capture above exception, we must write our
except clause as follows:
try:
Business Logic here...
except "Invalid level!":
Exception handling here...
else:
Rest of the code here...
Exception

More Related Content

What's hot (20)

Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in python
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Exception handling in java
Exception handling  in javaException handling  in java
Exception handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
7.error management and exception handling
7.error management and exception handling7.error management and exception handling
7.error management and exception handling
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
Presentation1
Presentation1Presentation1
Presentation1
 
43c
43c43c
43c
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
exception handling in java
exception handling in java exception handling in java
exception handling in java
 

Similar to Exception (20)

Exception Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.pptException Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.ppt
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Py-Slides-9.ppt
Py-Slides-9.pptPy-Slides-9.ppt
Py-Slides-9.ppt
 
EXCEPTION HANDLING IN PYTHON For students .py.pptx
EXCEPTION  HANDLING IN PYTHON For students .py.pptxEXCEPTION  HANDLING IN PYTHON For students .py.pptx
EXCEPTION HANDLING IN PYTHON For students .py.pptx
 
Python Lecture 7
Python Lecture 7Python Lecture 7
Python Lecture 7
 
exceptioninpython.pptx
exceptioninpython.pptxexceptioninpython.pptx
exceptioninpython.pptx
 
ACP - Week - 9.pptx
ACP - Week - 9.pptxACP - Week - 9.pptx
ACP - Week - 9.pptx
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
exception handling.pptx
exception handling.pptxexception handling.pptx
exception handling.pptx
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
 
Exceptions
ExceptionsExceptions
Exceptions
 
41c
41c41c
41c
 
Z blue exception
Z blue   exceptionZ blue   exception
Z blue exception
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
 
Interface andexceptions
Interface andexceptionsInterface andexceptions
Interface andexceptions
 
Exception handling.pptx
Exception handling.pptxException handling.pptx
Exception handling.pptx
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 

More from Navaneethan Naveen (20)

Class inheritance 13 session - SHAN
Class inheritance 13 session - SHANClass inheritance 13 session - SHAN
Class inheritance 13 session - SHAN
 
Python session 12
Python session 12Python session 12
Python session 12
 
Python session 11
Python session 11Python session 11
Python session 11
 
V irtualisation.1
V irtualisation.1V irtualisation.1
V irtualisation.1
 
Python session.11 By Shanmugam
Python session.11 By ShanmugamPython session.11 By Shanmugam
Python session.11 By Shanmugam
 
Virtualisation-11
Virtualisation-11Virtualisation-11
Virtualisation-11
 
Networking session-4-final by aravind.R
Networking session-4-final by aravind.RNetworking session-4-final by aravind.R
Networking session-4-final by aravind.R
 
Networking session3
Networking session3Networking session3
Networking session3
 
WIN-ADCS-10
WIN-ADCS-10WIN-ADCS-10
WIN-ADCS-10
 
Python session 10
Python session 10Python session 10
Python session 10
 
Python multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugamPython multithreading session 9 - shanmugam
Python multithreading session 9 - shanmugam
 
Python session 8
Python session 8Python session 8
Python session 8
 
Win 8th
Win 8thWin 8th
Win 8th
 
Virtualization session 8
Virtualization session 8Virtualization session 8
Virtualization session 8
 
Virtualization session 7 by Gugan
Virtualization session 7 by GuganVirtualization session 7 by Gugan
Virtualization session 7 by Gugan
 
Python session 7 by Shan
Python session 7 by ShanPython session 7 by Shan
Python session 7 by Shan
 
Virtualization s4.1
Virtualization s4.1Virtualization s4.1
Virtualization s4.1
 
Python session 6
Python session 6Python session 6
Python session 6
 
Gpo windows(4)
Gpo windows(4)Gpo windows(4)
Gpo windows(4)
 
Windows session 5 : Basics of active directory
Windows session 5 : Basics of active directoryWindows session 5 : Basics of active directory
Windows session 5 : Basics of active directory
 

Recently uploaded

TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....rightmanforbloodline
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 

Recently uploaded (20)

TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 

Exception

  • 3. What is Exception?  An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.  In general, when a Python script encounters a situation that it can't cope with, it raises an exception..  When a Python script raises an exception, it must either handle the exception immediately otherwise it would terminate and come out.
  • 4. Exceptions Types EXCEPTION NAME DESCRIPTION ZeroDivisonError Raised when division or modulo by zero takes place for all numeric types. ImportError Raised when an import statement fails. IOError Raised when an input/ output operation fails, the open() function when trying to open a file that does not exist. SyntaxError Raised when there is an error in Python syntax. IndentationError Raised when indentation is not specified properly.
  • 5. Handling an exception If any suspicious code that may raise an exception suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.
  • 6. • SYNTAX: Here is simple syntax of try....except...else blocks: try: You do your operations here; ...................... except ExceptionI: If there is ExceptionI, then execute this block. except ExceptionII: If there is ExceptionII, then execute this block. ...................... else: If there is no exception then execute this block.
  • 7.  A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions.  Also provide a generic except clause, which handles any exception.  After the except clause(s), you can include an else-clause. The code in the else-block executes if the code in the try: block does not raise an exception.
  • 8. • Example try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!") except IOError: print "Error: can't find file or read data" else: print "Written content in the file successfully" fh.close() Output: Written content in the file successfully
  • 9. • The except clause with multiple exceptions: try: You do your operations here; ...................... except(Exception1, Exception2,...ExceptionN): If there is any exception from the given exception list, then execute this block. ...................... else: If there is no exception then execute this block.
  • 10. • The try-finally clause: The finally block is a place to put any code that must execute, whether the try-block raised an exception or not try: fh = open("testfile", "w") fh.write("This is my test file for exception handling!!") finally: print "Error: can't find file or read data" If you do not have permission to open the file in writing mode, Output: Error: can't find file or read data
  • 11. • Raising an exceptions: Raise an exceptions in several ways by using the raise statement. SYNTAX: raise Exception [ args ] Here, Exception is the type of exception (for example, NameError) and argument is a value for the exception argument. The argument is optional; if not supplied, the exception argument is None.
  • 12. • Example: level = 1 if level < 2: raise "Invalid level!", level # The code below to this would not be executed # if we raise the exception
  • 13. • In order to catch an exception, an "except" clause must refer to the same exception thrown either class object or simple string. For example, to capture above exception, we must write our except clause as follows: try: Business Logic here... except "Invalid level!": Exception handling here... else: Rest of the code here...