際際滷

際際滷Share 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
Ad

Recommended

Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
Errorhandlingbyvipulkendroyavidyalayacrpfmudkhed.pptx
xaybhagfsus
Python Exception Handling
Python Exception Handling
Megha V
Exception handling with python class 12.pptx
Exception handling with python class 12.pptx
PreeTVithule1
Chapter 13 exceptional handling
Chapter 13 exceptional handling
Praveen M Jigajinni
Python programming : Exceptions
Python programming : Exceptions
Emertxe Information Technologies Pvt Ltd
lecs101.pdfgggggggggggggggggggddddddddddddb
lecs101.pdfgggggggggggggggggggddddddddddddb
MrProfEsOr1
Python Lecture 7
Python Lecture 7
Inzamam Baig
Exception Handling in Python Programming.pptx
Exception Handling in Python Programming.pptx
vinayagrawal71
LECT 29- EXCEPTION HANDLING.pptx b.
LECT 29- EXCEPTION HANDLING.pptx b.
gjhp9927
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
Exception Handling.pptx
Exception Handling.pptx
Pavan326406
Python-exceptionHandling.pptx
Python-exceptionHandling.pptx
Karudaiyar Ganapathy
Exception handlingpdf
Exception handlingpdf
gandra jeeshitha
Exception Handling in python programming.pptx
Exception Handling in python programming.pptx
shririshsri
Download Wondershare Filmora Crack Latest 2025
Download Wondershare Filmora Crack Latest 2025
hyderik195
Adobe Photoshop 2025 Cracked Latest Version
Adobe Photoshop 2025 Cracked Latest Version
hyderik195
FL Studio Producer Edition Crack 24 + Latest Version [2025]
FL Studio Producer Edition Crack 24 + Latest Version [2025]
hyderik195
Errors and Exceptions - in - Python.pptx
Errors and Exceptions - in - Python.pptx
Vignan's Foundation for Science, Technology and Research (Deemed to be University)
Error and exception in python
Error and exception in python
junnubabu
Firoze_Errors_Exceptions in python__.ppt
Firoze_Errors_Exceptions in python__.ppt
urstrulysurya
Exception Handling in Python topic .pptx
Exception Handling in Python topic .pptx
mitu4846t
Python Exception handling using Try-Except-Finally
Python Exception handling using Try-Except-Finally
Vinod Srivastava
Exception handling in python
Exception handling in python
Lifna C.S
Python Exceptions Powerpoint Presentation
Python Exceptions Powerpoint Presentation
mitchellblack733
Python Unit II.pptx
Python Unit II.pptx
sarthakgithub
Exception Handling
Exception Handling
baabtra.com - No. 1 supplier of quality freshers
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
Python for beginners textbook slides ppt
Python for beginners textbook slides ppt
AnithaChristyAngelin
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674

More Related Content

Similar to Exception handling3.pdf (20)

LECT 29- EXCEPTION HANDLING.pptx b.
LECT 29- EXCEPTION HANDLING.pptx b.
gjhp9927
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
Exception Handling.pptx
Exception Handling.pptx
Pavan326406
Python-exceptionHandling.pptx
Python-exceptionHandling.pptx
Karudaiyar Ganapathy
Exception handlingpdf
Exception handlingpdf
gandra jeeshitha
Exception Handling in python programming.pptx
Exception Handling in python programming.pptx
shririshsri
Download Wondershare Filmora Crack Latest 2025
Download Wondershare Filmora Crack Latest 2025
hyderik195
Adobe Photoshop 2025 Cracked Latest Version
Adobe Photoshop 2025 Cracked Latest Version
hyderik195
FL Studio Producer Edition Crack 24 + Latest Version [2025]
FL Studio Producer Edition Crack 24 + Latest Version [2025]
hyderik195
Errors and Exceptions - in - Python.pptx
Errors and Exceptions - in - Python.pptx
Vignan's Foundation for Science, Technology and Research (Deemed to be University)
Error and exception in python
Error and exception in python
junnubabu
Firoze_Errors_Exceptions in python__.ppt
Firoze_Errors_Exceptions in python__.ppt
urstrulysurya
Exception Handling in Python topic .pptx
Exception Handling in Python topic .pptx
mitu4846t
Python Exception handling using Try-Except-Finally
Python Exception handling using Try-Except-Finally
Vinod Srivastava
Exception handling in python
Exception handling in python
Lifna C.S
Python Exceptions Powerpoint Presentation
Python Exceptions Powerpoint Presentation
mitchellblack733
Python Unit II.pptx
Python Unit II.pptx
sarthakgithub
Exception Handling
Exception Handling
baabtra.com - No. 1 supplier of quality freshers
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
Python for beginners textbook slides ppt
Python for beginners textbook slides ppt
AnithaChristyAngelin
LECT 29- EXCEPTION HANDLING.pptx b.
LECT 29- EXCEPTION HANDLING.pptx b.
gjhp9927
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
EXCEPTIONS-PYTHON.pptx RUNTIME ERRORS HANDLING
NagarathnaRajur2
Exception Handling.pptx
Exception Handling.pptx
Pavan326406
Exception Handling in python programming.pptx
Exception Handling in python programming.pptx
shririshsri
Download Wondershare Filmora Crack Latest 2025
Download Wondershare Filmora Crack Latest 2025
hyderik195
Adobe Photoshop 2025 Cracked Latest Version
Adobe Photoshop 2025 Cracked Latest Version
hyderik195
FL Studio Producer Edition Crack 24 + Latest Version [2025]
FL Studio Producer Edition Crack 24 + Latest Version [2025]
hyderik195
Error and exception in python
Error and exception in python
junnubabu
Firoze_Errors_Exceptions in python__.ppt
Firoze_Errors_Exceptions in python__.ppt
urstrulysurya
Exception Handling in Python topic .pptx
Exception Handling in Python topic .pptx
mitu4846t
Python Exception handling using Try-Except-Finally
Python Exception handling using Try-Except-Finally
Vinod Srivastava
Exception handling in python
Exception handling in python
Lifna C.S
Python Exceptions Powerpoint Presentation
Python Exceptions Powerpoint Presentation
mitchellblack733
Python Unit II.pptx
Python Unit II.pptx
sarthakgithub
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
33aa27cae9c84fd12762a4ecdc288df822623524-1705207147822.ppt
svijaycdac
Python for beginners textbook slides ppt
Python for beginners textbook slides ppt
AnithaChristyAngelin

Recently uploaded (20)

Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
June 2025 Progress Update With Board Call_In process.pptx
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
Paper 107 | From Watchdog to Lapdog: Ishiguros Fiction and the Rise of Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguros Fiction and the Rise of Godi...
Rajdeep Bavaliya
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
HistoPathology Ppt. Arshita Gupta for Diploma
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
Birnagar High School Platinum Jubilee Quiz.pptx
Birnagar High School Platinum Jubilee Quiz.pptx
Sourav Kr Podder
Paper 107 | From Watchdog to Lapdog: Ishiguros Fiction and the Rise of Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguros Fiction and the Rise of Godi...
Rajdeep Bavaliya
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
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