SlideShare a Scribd company logo
Exceptions
Definition
An exception is a runtime error which can be handled by the
programmer. That means if the programmer can guess an error in the
program and he can do something to eliminate the harm caused by
that error then it is called an exception. If error is not handled by
programmer then it is called as error.
All exceptions are represented as classes in python. The exceptions
which are already available in python are called built-in exception.
Important exception classes in python
BaseException
Exception
StandardError Warning
DeprecationWarning
RuntimeWarning
ImportWarning
AssertionError
SyntaxError
TypeError
EOFError
RuntimeError
ImportError
ArthmeticError
NameError
Exception Handling
Purpose of handling errors is to make program robust. It means strong.
Robust program does not terminate in the middle. When there is a error
displays appropriate message to the user and continue execution. When
the errors can be handled they are called exception.
To handle the exception the programmer should perform the following
three steps
Step1: The statement that may cause the exception must be written inside
a ‘try’ block
try:
statements
If statement raises an error then it enters into except block otherwise
continues the execution
continued
Step2:Programmer should write except block where we should display the exception details to the
user
except exception name:
statements #these statements from handler
Step3:Lastly the programmer should perform clean up actions like closing the files and terminating
any other processes which are running.
finally:
statements
Statements inside the finally block are executed irrespective of whether there is an exception or
not. This ensures that all the opened files are properly closed and all the running processes are
terminated properly.
Syntax for handling exception
try:
statements
except Exception1:
exception handler1
except Exception2:
exception handler2
else:
statements
finally:
statements
Types of Exception
Exception Class Name Description
Exception Represents any type of exception. All exception are sub classes of this class
ArithmeticError Represents the base class for arithmetic errors like OverflowError,
ZeroDivisionError, FloatingPointError
AssertionError Raised when an assert statement gives error
AttributeError Raised when an attribute reference or assignment fails
EOFError Raised when input() function reaches end of file condition without reading any
data
FloatingPointError Raised when a floating operation fails
GeneratorExit Raised when generators close() method is called
IOError Raised when an input or output operation failed. It raises when the file opened is
not found or when writing data disk is full
ImportError Raised when an import statement fails to find the module being imported
continued
Exception Class Name Description
IndexError Raised when a sequence index or subscript is out of range
KeyError Raised when a mapping (dictionary) key is not found in the set of existing
keys
KeyboardInterrupt Raised when the user hits the interrupt key(Control-C, Delete)
NameError Raised when an identifier is not found locally or globally
NotImplementedError Derived from runtimeError
Programs on exception
Python program to handle ZeroDivisionError exception
Filename:ep1.py
Python program to handle syntax error given by eval() function
Filename:ep2.py
Python program to handle multiple exception
Filename:ep3.py
User Defined Exception
Like the built in exceptions of python user can also create his own
exceptions which are called user defined exception or custom exception.
EXAMPLE: Matainance of minimum balance in account
Steps to define user defined exception
1.Create own exception class and is sub class of built in class Exception
class MyExecption(Exception):
def __init__(self,arg):
self.msg=arg
msg is member of class MyException and is assigned with parameter
received from outside
Continued
2. When the programmer suspects the possibility of exception raise own
exception using raise statement
Raise MyExecption(‘message’)
3. Programmer must write the code inside a try block and catch the
exception using except block as
try:
code
except MyException as me
print(me)
Python program to illustrate user defined exception
Filename:ep4.py
THANK YOU

More Related Content

PPTX
Exception Handling in Python programming.pptx
vinayagrawal71
 
PPTX
Exception Handling in Python Programming.pptx
vinayagrawal71
 
PPTX
Exception handling with python class 12.pptx
PreeTVithule1
 
PPTX
Exception Handling in python programming.pptx
shririshsri
 
PPT
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
PDF
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
DOCX
Exception handlingpdf
gandra jeeshitha
 
PDF
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 
Exception Handling in Python programming.pptx
vinayagrawal71
 
Exception Handling in Python Programming.pptx
vinayagrawal71
 
Exception handling with python class 12.pptx
PreeTVithule1
 
Exception Handling in python programming.pptx
shririshsri
 
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
 
Unit 4-Exception Handling in Python.pdf
Harsha Patil
 
Exception handlingpdf
gandra jeeshitha
 
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
 

Similar to EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING (20)

PDF
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
 
PPTX
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
 
PPTX
Python Unit II.pptx
sarthakgithub
 
PPTX
Python Exception Handling
Megha V
 
PPT
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
PPTX
Python Lecture 7
Inzamam Baig
 
PPTX
Python_Exception_Handling_Presentation.pptx
csanilram
 
PPTX
Exception Handling.pptx
Pavan326406
 
PPT
Exception
Navaneethan Naveen
 
PPTX
Exception handling.pptxnn h
sabarivelan111007
 
PDF
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
PPTX
Python Exceptions Powerpoint Presentation
mitchellblack733
 
PPTX
Exception handling.pptx
NISHASOMSCS113
 
PDF
Exception handling in python
Lifna C.S
 
PPTX
Error and exception in python
junnubabu
 
PPTX
Chapter 13 exceptional handling
Praveen M Jigajinni
 
PPTX
Exception Handling in Python topic .pptx
mitu4846t
 
PPT
Exception handling in python and how to handle it
s6901412
 
PPTX
Python Exception Handling (Python Course)
IGustiNyomanAntonSur
 
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
 
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
 
Python Unit II.pptx
sarthakgithub
 
Python Exception Handling
Megha V
 
Exception Handling on 22nd March 2022.ppt
Raja Ram Dutta
 
Python Lecture 7
Inzamam Baig
 
Python_Exception_Handling_Presentation.pptx
csanilram
 
Exception Handling.pptx
Pavan326406
 
Exception handling.pptxnn h
sabarivelan111007
 
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 
Python Exceptions Powerpoint Presentation
mitchellblack733
 
Exception handling.pptx
NISHASOMSCS113
 
Exception handling in python
Lifna C.S
 
Error and exception in python
junnubabu
 
Chapter 13 exceptional handling
Praveen M Jigajinni
 
Exception Handling in Python topic .pptx
mitu4846t
 
Exception handling in python and how to handle it
s6901412
 
Python Exception Handling (Python Course)
IGustiNyomanAntonSur
 
Ad

More from NagarathnaRajur2 (20)

PPT
Basics of C (2).ppt bbbbbbbbbbbbbbbbbbbbb
NagarathnaRajur2
 
PPT
C-Programming Chapter 1 Fundamentals of C.ppt
NagarathnaRajur2
 
PPT
Basics of C.ppt VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
NagarathnaRajur2
 
PPT
C-Programming Chapter 1 Fundamentals of C.ppt
NagarathnaRajur2
 
PPT
lecture-ON-C.ppt BASIC WITH DEPTH CONTENT
NagarathnaRajur2
 
PPTX
OPERATORS-PYTHON.pptx ALL OPERATORS ARITHMATIC AND LOGICAL
NagarathnaRajur2
 
PPTX
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
PPTX
ppt on arm memory.pptx yjjghjghjjjjjjjj
NagarathnaRajur2
 
PPTX
05_Thumb (15).pptx ARM MICROCONTROLLERS THUMB INSTRUCTIONS WORKING PRINCIPLE
NagarathnaRajur2
 
PPT
PM.ppt DATA STRUCTURE USING C WITH EXAMPLE PROGRAMES
NagarathnaRajur2
 
PPTX
8051 programes -ppt.pptx bnvmmmmmmmmmmmmmmmmmmmmmmmmmmhn
NagarathnaRajur2
 
PPTX
Chapter_04_ARM_Assembly.pptx ARM ASSEMBLY CODE
NagarathnaRajur2
 
PPT
MEMORY.ppt 8051/8052 MEMORY MANEGEMENT MEMORY DESCRIPTION
NagarathnaRajur2
 
PPTX
CHAPTER1.pptx ON 8051 MICROCONTROLLER INTRODUCTION CHAPTER
NagarathnaRajur2
 
PPTX
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
NagarathnaRajur2
 
PPTX
review.pptx mnbmnbm,nb,n,nm,mn,mn,n,nm,
NagarathnaRajur2
 
PPTX
IOT introduction with applications ffffffffffffffffffffff
NagarathnaRajur2
 
PPTX
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
PPTX
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
PPTX
JavaSteps. PPT NBNBVNBNVBNNNNNNNNNNNNNN
NagarathnaRajur2
 
Basics of C (2).ppt bbbbbbbbbbbbbbbbbbbbb
NagarathnaRajur2
 
C-Programming Chapter 1 Fundamentals of C.ppt
NagarathnaRajur2
 
Basics of C.ppt VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
NagarathnaRajur2
 
C-Programming Chapter 1 Fundamentals of C.ppt
NagarathnaRajur2
 
lecture-ON-C.ppt BASIC WITH DEPTH CONTENT
NagarathnaRajur2
 
OPERATORS-PYTHON.pptx ALL OPERATORS ARITHMATIC AND LOGICAL
NagarathnaRajur2
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
ppt on arm memory.pptx yjjghjghjjjjjjjj
NagarathnaRajur2
 
05_Thumb (15).pptx ARM MICROCONTROLLERS THUMB INSTRUCTIONS WORKING PRINCIPLE
NagarathnaRajur2
 
PM.ppt DATA STRUCTURE USING C WITH EXAMPLE PROGRAMES
NagarathnaRajur2
 
8051 programes -ppt.pptx bnvmmmmmmmmmmmmmmmmmmmmmmmmmmhn
NagarathnaRajur2
 
Chapter_04_ARM_Assembly.pptx ARM ASSEMBLY CODE
NagarathnaRajur2
 
MEMORY.ppt 8051/8052 MEMORY MANEGEMENT MEMORY DESCRIPTION
NagarathnaRajur2
 
CHAPTER1.pptx ON 8051 MICROCONTROLLER INTRODUCTION CHAPTER
NagarathnaRajur2
 
Core-Java-by-Mahika-Tutor.9459891.powerpoint.pptx
NagarathnaRajur2
 
review.pptx mnbmnbm,nb,n,nm,mn,mn,n,nm,
NagarathnaRajur2
 
IOT introduction with applications ffffffffffffffffffffff
NagarathnaRajur2
 
CORE JAVA PPT FOR ENGINEERS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
JAVA CLASS PPT FOR ENGINEERING STUDENTS BBBBBBBBBBBBBBBBBBB
NagarathnaRajur2
 
JavaSteps. PPT NBNBVNBNVBNNNNNNNNNNNNNN
NagarathnaRajur2
 
Ad

Recently uploaded (20)

PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Understanding operators in c language.pptx
auteharshil95
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Landforms and landscapes data surprise preview
jpinnuck
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 

EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING

  • 2. Definition An exception is a runtime error which can be handled by the programmer. That means if the programmer can guess an error in the program and he can do something to eliminate the harm caused by that error then it is called an exception. If error is not handled by programmer then it is called as error. All exceptions are represented as classes in python. The exceptions which are already available in python are called built-in exception.
  • 3. Important exception classes in python BaseException Exception StandardError Warning DeprecationWarning RuntimeWarning ImportWarning AssertionError SyntaxError TypeError EOFError RuntimeError ImportError ArthmeticError NameError
  • 4. Exception Handling Purpose of handling errors is to make program robust. It means strong. Robust program does not terminate in the middle. When there is a error displays appropriate message to the user and continue execution. When the errors can be handled they are called exception. To handle the exception the programmer should perform the following three steps Step1: The statement that may cause the exception must be written inside a ‘try’ block try: statements If statement raises an error then it enters into except block otherwise continues the execution
  • 5. continued Step2:Programmer should write except block where we should display the exception details to the user except exception name: statements #these statements from handler Step3:Lastly the programmer should perform clean up actions like closing the files and terminating any other processes which are running. finally: statements Statements inside the finally block are executed irrespective of whether there is an exception or not. This ensures that all the opened files are properly closed and all the running processes are terminated properly.
  • 6. Syntax for handling exception try: statements except Exception1: exception handler1 except Exception2: exception handler2 else: statements finally: statements
  • 7. Types of Exception Exception Class Name Description Exception Represents any type of exception. All exception are sub classes of this class ArithmeticError Represents the base class for arithmetic errors like OverflowError, ZeroDivisionError, FloatingPointError AssertionError Raised when an assert statement gives error AttributeError Raised when an attribute reference or assignment fails EOFError Raised when input() function reaches end of file condition without reading any data FloatingPointError Raised when a floating operation fails GeneratorExit Raised when generators close() method is called IOError Raised when an input or output operation failed. It raises when the file opened is not found or when writing data disk is full ImportError Raised when an import statement fails to find the module being imported
  • 8. continued Exception Class Name Description IndexError Raised when a sequence index or subscript is out of range KeyError Raised when a mapping (dictionary) key is not found in the set of existing keys KeyboardInterrupt Raised when the user hits the interrupt key(Control-C, Delete) NameError Raised when an identifier is not found locally or globally NotImplementedError Derived from runtimeError
  • 9. Programs on exception Python program to handle ZeroDivisionError exception Filename:ep1.py Python program to handle syntax error given by eval() function Filename:ep2.py Python program to handle multiple exception Filename:ep3.py
  • 10. User Defined Exception Like the built in exceptions of python user can also create his own exceptions which are called user defined exception or custom exception. EXAMPLE: Matainance of minimum balance in account Steps to define user defined exception 1.Create own exception class and is sub class of built in class Exception class MyExecption(Exception): def __init__(self,arg): self.msg=arg msg is member of class MyException and is assigned with parameter received from outside
  • 11. Continued 2. When the programmer suspects the possibility of exception raise own exception using raise statement Raise MyExecption(‘message’) 3. Programmer must write the code inside a try block and catch the exception using except block as try: code except MyException as me print(me) Python program to illustrate user defined exception Filename:ep4.py