SlideShare a Scribd company logo
5
Most read
6
Most read
7
Most read
Chapter 3
Exception handling
New
syllabus
2023-24
Visit : python.mykvs.in for regular updates
Exception Handling
Error in Python can be of two types i.e. Syntax
errors and Exceptions. Errors are problems in a
program due to which will stop the program from
execution. On the other hand, exceptions are raised
when some internal events occur due to limitation
of hardware or software part, which change the
normal flow of the program.
Visit : python.mykvs.in for regular updates
Different types of exceptions in python:
• SyntaxError: This exception is raised when the interpreter encounters a syntax error in
the code, such as a misspelled keyword, a missing colon, or an unbalanced parenthesis.
• TypeError: This exception is raised when an operation or function is applied to an object
of the wrong type, such as adding a string to an integer.
• NameError: This exception is raised when a variable or function name is not found in
the current scope.
• IndexError: This exception is raised when an index is out of range for a list, tuple, or
other sequence types.
• KeyError: This exception is raised when a key is not found in a dictionary.
• ValueError: This exception is raised when a function or method is called with an invalid
argument or input, such as trying to convert a string to an integer when the string does
not represent a valid integer.
• AttributeError: This exception is raised when an attribute or method is not found on an
object, such as trying to access a non-existent attribute of a class instance.
• IOError: This exception is raised when an I/O operation, such as reading or writing a
file, fails due to an input/output error.
• ZeroDivisionError: This exception is raised when an attempt is made to divide a number
by zero.
• ImportError: This exception is raised when an import statement fails to find or load a
module.
Visit : python.mykvs.in for regular updates
Difference between Syntax Error and
Exceptions
Syntax Error: This error is caused by the wrong syntax in the
code.
if(amount > 999)
print(“amount more than 1000")
if(amount > 999)
Exceptions: Exceptions are raised when the program is syntactically
correct, but the code results in an error. This error does not stop the
execution of the program, however, it changes the normal flow of the
program.
a = 10 / 0
print(a)
Visit : python.mykvs.in for regular updates
handling exceptions using try-except-
finally blocks
try:
# Some Code....which may have runtime error
except:
# optional block
# Handling of exception (if required)
else:
# execute if no exception
finally:
# Some code .....(always executed)
Visit : python.mykvs.in for regular updates
try:
k = 9//0 # raises divide by zero exception.
print(k)
# handles zerodivision exception
except ZeroDivisionError:
print("Can't divide by zero")
finally:
# this block is always executed
# regardless of exception generation.
print('This is always executed')
Visit : python.mykvs.in for regular updates
Advantages of Exception Handling:
• Improved program reliability
• Simplified error handling
• Cleaner code
• Easier debugging
Disadvantages of Exception Handling:
• Performance overhead
• Increased code complexity
• Possible security risks
Visit : python.mykvs.in for regular updates

More Related Content

Similar to Exception handling3.pdf (20)

PPTX
Exception handling
Minal Maniar
 
PPTX
Python Session - 6
AnirudhaGaikwad4
 
PPTX
Exception and its handling mechanism.pptx
maheswarancs1
 
PDF
Tutorial on-python-programming
Chetan Giridhar
 
PPTX
Exception handling in .net
baabtra.com - No. 1 supplier of quality freshers
 
PDF
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
 
PPT
Exception handling
pooja kumari
 
PPTX
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Design byexceptions
Asif Tasleem
 
PPTX
Exception Handling in Python topic .pptx
mitu4846t
 
PPTX
Python for beginners textbook slides ppt
AnithaChristyAngelin
 
PPT
S D D Program Development Tools
gavhays
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
PPTX
UNIT III 2021R.pptx
RDeepa9
 
PPTX
Exception handling in Java
Ankit Rai
 
PPTX
Event handling
Mohamed Essam
 
PPTX
Python Unit II.pptx
sarthakgithub
 
PPT
Exception
abhay singh
 
PPTX
Advanced python
Novita Sari
 
PPTX
ACP - Week - 9.pptx
funnyvideosbysam
 
Exception handling
Minal Maniar
 
Python Session - 6
AnirudhaGaikwad4
 
Exception and its handling mechanism.pptx
maheswarancs1
 
Tutorial on-python-programming
Chetan Giridhar
 
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
 
Exception handling
pooja kumari
 
Exception handling in ASP .NET
baabtra.com - No. 1 supplier of quality freshers
 
Design byexceptions
Asif Tasleem
 
Exception Handling in Python topic .pptx
mitu4846t
 
Python for beginners textbook slides ppt
AnithaChristyAngelin
 
S D D Program Development Tools
gavhays
 
UNIT III 2021R.pptx
RDeepa9
 
UNIT III 2021R.pptx
RDeepa9
 
Exception handling in Java
Ankit Rai
 
Event handling
Mohamed Essam
 
Python Unit II.pptx
sarthakgithub
 
Exception
abhay singh
 
Advanced python
Novita Sari
 
ACP - Week - 9.pptx
funnyvideosbysam
 

Recently uploaded (20)

PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
DOCX
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Views on Education of Indian Thinkers J.Krishnamurthy..pptx
ShrutiMahanta1
 
Ad

Exception handling3.pdf

  • 1. Chapter 3 Exception handling New syllabus 2023-24 Visit : python.mykvs.in for regular updates
  • 2. Exception Handling Error in Python can be of two types i.e. Syntax errors and Exceptions. Errors are problems in a program due to which will stop the program from execution. On the other hand, exceptions are raised when some internal events occur due to limitation of hardware or software part, which change the normal flow of the program. Visit : python.mykvs.in for regular updates
  • 3. Different types of exceptions in python: • SyntaxError: This exception is raised when the interpreter encounters a syntax error in the code, such as a misspelled keyword, a missing colon, or an unbalanced parenthesis. • TypeError: This exception is raised when an operation or function is applied to an object of the wrong type, such as adding a string to an integer. • NameError: This exception is raised when a variable or function name is not found in the current scope. • IndexError: This exception is raised when an index is out of range for a list, tuple, or other sequence types. • KeyError: This exception is raised when a key is not found in a dictionary. • ValueError: This exception is raised when a function or method is called with an invalid argument or input, such as trying to convert a string to an integer when the string does not represent a valid integer. • AttributeError: This exception is raised when an attribute or method is not found on an object, such as trying to access a non-existent attribute of a class instance. • IOError: This exception is raised when an I/O operation, such as reading or writing a file, fails due to an input/output error. • ZeroDivisionError: This exception is raised when an attempt is made to divide a number by zero. • ImportError: This exception is raised when an import statement fails to find or load a module. Visit : python.mykvs.in for regular updates
  • 4. Difference between Syntax Error and Exceptions Syntax Error: This error is caused by the wrong syntax in the code. if(amount > 999) print(“amount more than 1000") if(amount > 999) Exceptions: Exceptions are raised when the program is syntactically correct, but the code results in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program. a = 10 / 0 print(a) Visit : python.mykvs.in for regular updates
  • 5. handling exceptions using try-except- finally blocks try: # Some Code....which may have runtime error except: # optional block # Handling of exception (if required) else: # execute if no exception finally: # Some code .....(always executed) Visit : python.mykvs.in for regular updates
  • 6. try: k = 9//0 # raises divide by zero exception. print(k) # handles zerodivision exception except ZeroDivisionError: print("Can't divide by zero") finally: # this block is always executed # regardless of exception generation. print('This is always executed') Visit : python.mykvs.in for regular updates
  • 7. Advantages of Exception Handling: • Improved program reliability • Simplified error handling • Cleaner code • Easier debugging Disadvantages of Exception Handling: • Performance overhead • Increased code complexity • Possible security risks Visit : python.mykvs.in for regular updates