0% found this document useful (0 votes)
2 views

unit 3 pp

Uploaded by

.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unit 3 pp

Uploaded by

.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Ecceptioo handioq

Syotax CYSDES: Pstgsammexs


may typdq sapbical
soDSS whe
editinq po0qsamSPythtn
oteop setes wi) alsDy
detect
aul 0ayS hem Such
Such
esdbss ade Called Sunt a
esobES
eng th iop ul (" eotex")
>

>
porot ( leng cssD
length ioput eotes
=

)
pint (lenqth) Syotaz e
CSpace
3) > 3+
(Syotae esvs)
6cception 6ssors
Ay p8oqEam ming
Language has
tpes of
es sts
) Syntac essos
Essdss due to iovalio
Syotac
2 Ruo-time essoss essDoS
dusing N
Sn t)me f
a pobgdam

ELceptibns abe sun- time


esSDSS
AO uowat ed, unecpectool event that distusbs
oSmau)fow of the
psbqsam is Caed exCept idc
E )P8iot (1o/p) 2division by Dxceptivo
Fopen ("c.txt, "s)
CSED when e t e t is not avaulable
ile oot found exceptida .
Eeception handling when we get a ecceptitn
t wl distusb the oömal flow of
ececud
I t wi) tesminate the
popgam So these sh;hduld
be altesnative wo to Continue he xest
an
PS0osam erceCution he Concept of ecce,
andiaContinue the sest of Poq am eceCnbite
allows us to
though these is an ezceptiOn
Benefits of ezcept ion handli nq
9TE is usedto Continue he ffow of ecetion
oithout tesminatinq the poog dan abrosmally
h e n ecception is encoumte&cd.
2)fie the sun-time esbs.
3)-fe Sepaßabes esso8 Code usinq bay and
eccept b/ocks
Defaut ¬rception hadling in Pytn
psit (1D/ o)
we Du ecCube his oe ou will qet
Ccception bcoz oe ase
toying to djvide a
nD byohese is a de fault ecceptio0
handler which wil hanol)e the exceptons
ave us the output with thak
Tsouce back
er CEptiv.
0P
pivision by zeso
Ecceptio0 hiexaschy
Bose ecCeption
CxceptiDn Systemexit Genesat ox key boa
cit
oteraupt
ALtrbube Axth metic Cxxx T
zesd oljvisioocsbs CoF Name
OS
ookup float ioq polnt ¬sobs Val ue
Fovesflouo essd esboo
-Fileodt
oderessba | fund.es0
key esbs -jotessuptal
pemison
essos
Handing ecceptions Timedwt
TO handle ecceptivns e have 3
keydosdLs
() eccept (a)tinaly
t Sunta
isky Code to be ececuted
Cccept ¬xceptivoClass
handlinq Code
final
Cleon up Code
PXne 10/)
except Zeso D ViSipn ESo
paiot ("p visico b 2eSD
NO
s erceptipn
CCuS ed
yes
eccept block
Case 1 ecceptido
Stabe St mt 2,3 exe uute
Stmt 2
CaSe 2 : Exceptido 2
Stmt 3
Stmt1,y ase
execute
eccept
Stmt CaSe 3 Tf hese is nD Cxce
olock éhexe Ccceptidn
DCk
Cause abn odma tes minati
CaSe u exception io Stmts
Causes abodS mal t esmnt
Eccept Can be used co pooE ExCeptito
otosmati on as follo os
poot (12/o)
erCept
ecept 2eð5
DiVisipo Essos as sq
psiot (msSq)
o/p DiVisiD0 by zedo
Ty ot h
multiple etcepPE
etcept block
aiot ( input
("eotes a
value)
value ))
b ot ioput (" ent ex b
value"))
Pint (a/s)
Eccept 2edd Divisibn Es808
Point ("cant divi de by
zcx o)
eccept value Essos
Pint("eot es ioteges value)
Case 1
Case2
bID
o/p b1D
o/p Cont divide by zero
Case3' a10D
bfve
olp entes ioteqes value
Srage except
Sogle block handl es multiple
ecceptions
C
eccept( zesd Di visi Do ¬SS Da, Value exsos)as msg
psiotexsos OCCused', mSq)
Soge
Default Eccept block
C To handle of esSDS
any 4pe we Can u
default except b/ock
eccept:
piotcrbs OCCussed)
Vaudiatidns of ¬xcept
e c cept ECCeptionclass
2) eccepe Ecception Class as msq
aS ms
3) cecept (¬xceclassI, Class2)
u) eccept (
as mSq
5) eccept
NDTEIf toy oith multiple eccept
blocks
available, then detault eccept block should
last obheswise e w I 9eE Syntac ersox
fioally : Tt is aa bbck always ossoCjated
oith t5y 2 except to maintain Clean up cde
Tt is always excuted issespecti ve ot
ethes ecception saisSed ds not daise
is
and hethe handled 0 oo handJed.
Whatcves oesouSCES ase aldcaked in the
block Con be dealocat ed by fioally b/ech. -
toy
Code
yes
ND cceptior)
OCCusedl
yes
NO
Crcept 1on
handed
Finally
blook
Nested tsy-ecCpt- tinaly blocks
we Can de fine tsy-ecepttioaNY b/ocks
inside the ty, e x cept 2 tinal!y blbcks.
stmt
Stmt 2
St mt 3
eccept
Stmt 4
finally
stmts
CcceptE
St mtG
tioal
Stmt7
pint(tsy block)
poot(12/o)
eccept
p'ot( Di vide bby zcr)
final handled by inner
piot ( exceptidn
tsy block)
eccept :
Point oukex except blbck )
fina
JDck
psi ot ("dutes -fioally
Dutput-
e5 block
Divide by 2e30
innes txy block.
eception handled by
oukes finally blbck
when ecception is ised jn onex b/ock
one eccept block w) handle erceptoo
unable to ban olle, hen
outes hamle
1 else-clause along with toy -except-T C C C e p e - f i n
block e Can also use else - clause
ese blhck will be exe cut ed , when tXy
block dnes not daise ay exCept
pohot ha"
Cept
Poit Co8b8 ) Oukput ho
NO CXsD
CIse
ppiotND exSos) jna
finally
pslot+nallu'
RayS ng exceptipns Rajse stmt s used to
aise the
toscefu)y by he uses
alhes than yth on vi&t ua) Machine (PVM)
C =
int (ioput ("entex
value")
f x<D
aise Eccepti Do(ente
numbe >
Types of ¬xceptions
Thebe ase >tupes of erceptivos i
U pythto
1) pse detioed/ Buit -io exceptions
hen the ecceptioo saisea io python pödq>am
pVm
PV Coeakes erceptivo object & Stops the
Pöbasa exe Cutib gives the intosmatidrn to
the pobqsammes
C zestDiViSion Cs68, FileNDE FoundExrox,
etc.
ves de fine d Ecceptions/ customized
eplL)uns. Us e & 03 p qaammes Can SaiSe his
oon deal oith Commo
Ccceptioos to
PDblemS Such as iovalid pjo es80&y wSoro
details evsos, etc.
tOsite the ist of, pylthon exceptid
pedeti ned
S No Nam of exception Pesciptine
Eccoption This is the bo
Closs of al
ecceptions
Asithmatic essva a mathemabical
Compubation Cssx
bebngs bo this cla
zeso dVisoo es&0s when you divideo
noby zesd.
Uses detined Ecceptions
Python als o lets usey to cseate his own
eccetions koouon as uses- detined eccepbi
0 Customizedl ecccptions 7d Coeabe a
Customi zed ecception we the
wse
help o
predef ned eccept)ons
E Clo ss
Ernpty Essd8 (Run Time Esso)
def -ioit- (Self,
asqument)
SelF.asq uments = asq umeE
Vas
Saise Emptu ¬ssvs (" The vasiable Sn
eccept(Empty¬sss, va)
pint( Vas asqument
Detioin9 Cleanup ouctions in Pythbo
e opeo(" xtrt", "w")
file.8itel" Testing")
pint("ositing to fils')
eccept Io¬srd:
pint("couwld nut wsite to atile
ese
point(" D& te Success ful
final'y
ile close )
piot (" file closed)
olp
wsieinq to tile
D8ite Success tul
file closed
"
file =
opeo ("c . tct,
file wste ( Testinq")
poiot wsitinq to file)
CcceptE ToE sSox
(Same as Er 1)
CDuld not oite to a file
file clos ed
tileopen "c.tet", "w
file. w&ite (u
(Same as)
oP
NOTE
Ohebhex the ecception is aised os not, tho
Clean uP actio0 (finally block) il ke
ececui
fisst & then the CooD is
oaised by the
Compile98
Pedetioed Clean up actioos: Some o
bjects
detine standasd clean up actions to be
wndestaken ohen he object is nd
needeod
longes
seqasdljess of whethex os ntt be
opesat ion usiog the okject tai led.
C fos Line in opeo (
tet):
po oe (Line, epd )
The pooblem t h thiS CodeE wl) leave
the file Open This mau o be a
pobble
Smaulles applicabions but when we aðe
itinq Lasqe he fle has to be close
De use belDu Cvde,
Eh 0pen( " . t x t ) aS F
fo Li ne in f
Piot(Lioe, ed
he
The ith statement allows obyeds
Hal
i Ke
files tD be used n a uay
enS udes
ensuses hey
hey ase always Cleaned
uP
up
potmptly& Co dectly
ASsestionsAn Assestion is a San, ty
Check that e Can tusn on o otf f when
Le ase done with testinq of the poogSam. e
Se aSseStjDns hen oe ane to ealuate
Hhe e p it he ecpsession is u e , these
,
will be
be n o ezception but if e<p is false
assextion wil Sons an eceptidn
use
assest keuo0sa to saise as seations
ASS estions ase uSed fsbm pyth on vexsio
def kF( Temp)
assesE( Temp= O) "colde3 than
absd ute z e o
etusn Temp -273)*8) +3
poiot kF(273 3 2-0
POint ( ot (kF( S05. 78 ) ) 45)
piotkF (-5)) coldes than oubsdluke zest

You might also like