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
PPTX
Exception Handling in Python Programming.pptx
PPTX
Exception handling with python class 12.pptx
PPTX
Exception Handling in python programming.pptx
PPT
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
PDF
Unit 4-Exception Handling in Python.pdf
DOCX
Exception handlingpdf
PDF
Python programming : Exceptions
Exception Handling in Python programming.pptx
Exception Handling in Python Programming.pptx
Exception handling with python class 12.pptx
Exception Handling in python programming.pptx
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
Unit 4-Exception Handling in Python.pdf
Exception handlingpdf
Python programming : Exceptions

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

PDF
lecs101.pdfgggggggggggggggggggddddddddddddb
PPTX
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
PPTX
Python Unit II.pptx
PPTX
Python Exception Handling
PPT
Exception Handling on 22nd March 2022.ppt
PPTX
Python Lecture 7
PPTX
Python_Exception_Handling_Presentation.pptx
PPTX
Exception Handling.pptx
PPT
PPTX
Exception handling.pptxnn h
PDF
Python Programming - X. Exception Handling and Assertions
PPTX
Python Exceptions Powerpoint Presentation
PPTX
Exception handling.pptx
PDF
Exception handling in python
PPTX
Error and exception in python
PPTX
Chapter 13 exceptional handling
PPTX
Exception Handling in Python topic .pptx
PPT
Exception handling in python and how to handle it
PPTX
Python Exception Handling (Python Course)
lecs101.pdfgggggggggggggggggggddddddddddddb
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Python Unit II.pptx
Python Exception Handling
Exception Handling on 22nd March 2022.ppt
Python Lecture 7
Python_Exception_Handling_Presentation.pptx
Exception Handling.pptx
Exception handling.pptxnn h
Python Programming - X. Exception Handling and Assertions
Python Exceptions Powerpoint Presentation
Exception handling.pptx
Exception handling in python
Error and exception in python
Chapter 13 exceptional handling
Exception Handling in Python topic .pptx
Exception handling in python and how to handle it
Python Exception Handling (Python Course)
Ad

More from NagarathnaRajur2 (20)

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

Recently uploaded (20)

PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
human mycosis Human fungal infections are called human mycosis..pptx
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Renaissance Architecture: A Journey from Faith to Humanism
O7-L3 Supply Chain Operations - ICLT Program
STATICS OF THE RIGID BODIES Hibbelers.pdf
NOI Hackathon - Summer Edition - GreenThumber.pptx
Pharma ospi slides which help in ospi learning
Week 4 Term 3 Study Techniques revisited.pptx
Revamp in MTO Odoo 18 Inventory - Odoo Slides
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

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