Exception handling3.pdf

Brokeass1 98 views 7 slides Oct 05, 2023
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

Python


Slide Content

Chapter 3
Exception handling
New
syllabus
2023-24
Visit : python.mykvs.in for regularupdates

Exception Handling
ErrorinPythoncanbeoftwotypesi.e.Syntax
errorsandExceptions.Errorsareproblemsina
programduetowhichwillstoptheprogramfrom
execution.Ontheotherhand,exceptionsareraised
whensomeinternaleventsoccurduetolimitation
ofhardwareorsoftwarepart,whichchangethe
normalflowoftheprogram.
Visit : python.mykvs.in for regular updates

Differenttypesofexceptionsinpython:
•SyntaxError:Thisexceptionisraisedwhentheinterpreterencountersasyntaxerrorin
thecode,suchasamisspelledkeyword,amissingcolon,oranunbalancedparenthesis.
•TypeError:Thisexceptionisraisedwhenanoperationorfunctionisappliedtoanobject
ofthewrongtype,suchasaddingastringtoaninteger.
•NameError:Thisexceptionisraisedwhenavariableorfunctionnameisnotfoundin
thecurrentscope.
•IndexError:Thisexceptionisraisedwhenanindexisoutofrangeforalist,tuple,or
othersequencetypes.
•KeyError:Thisexceptionisraisedwhenakeyisnotfoundinadictionary.
•ValueError:Thisexceptionisraisedwhenafunctionormethodiscalledwithaninvalid
argumentorinput,suchastryingtoconvertastringtoanintegerwhenthestringdoes
notrepresentavalidinteger.
•AttributeError:Thisexceptionisraisedwhenanattributeormethodisnotfoundonan
object,suchastryingtoaccessanon-existentattributeofaclassinstance.
•IOError:ThisexceptionisraisedwhenanI/Ooperation,suchasreadingorwritinga
file,failsduetoaninput/outputerror.
•ZeroDivisionError:Thisexceptionisraisedwhenanattemptismadetodivideanumber
byzero.
•ImportError:Thisexceptionisraisedwhenanimportstatementfailstofindorloada
module.
Visit : python.mykvs.in for regular updates

DifferencebetweenSyntaxErrorand
Exceptions
SyntaxError:Thiserroriscausedbythewrongsyntaxinthe
code.
if(amount>999)
print(“amountmorethan1000")
if(amount>999)
Exceptions:Exceptionsareraisedwhentheprogramissyntactically
correct,butthecoderesultsinanerror.Thiserrordoesnotstopthe
executionoftheprogram,however,itchangesthenormalflowofthe
program.
a=10/0
print(a)
Visit : python.mykvs.in for regular updates

handlingexceptionsusingtry-except-
finallyblocks
try:
#SomeCode....whichmayhaveruntimeerror
except:
#optionalblock
#Handlingofexception(ifrequired)
else:
#executeifnoexception
finally:
#Somecode.....(alwaysexecuted)
Visit : python.mykvs.in for regular updates

try:
k=9//0#raisesdividebyzeroexception.
print(k)
#handleszerodivisionexception
exceptZeroDivisionError:
print("Can'tdividebyzero")
finally:
#thisblockisalwaysexecuted
#regardlessofexceptiongeneration.
print('Thisisalwaysexecuted')
Visit : python.mykvs.in for regular updates

AdvantagesofExceptionHandling:
•Improvedprogramreliability
•Simplifiederrorhandling
•Cleanercode
•Easierdebugging
DisadvantagesofExceptionHandling:
•Performanceoverhead
•Increasedcodecomplexity
•Possiblesecurityrisks
Visit : python.mykvs.in for regular updates
Tags