COMPUTeR 4-09-2
cCHAP TER-1-Exaptíon tlondling
5 built fn fxaptons
Name Of the thh'Ereaptfon Sesetpton
TI fs arsed uhen thereS an TT0T n the
Syntax Syntax of python Code
Tt is Talsed when Local of gobadVanab
is not de ftoed
It (s malsed uohenthe denominatr in a
dirííonoperation is zero
Valwe H is raised when the built io funon
Te eves an argument in w°ch mis-matr
oata types e passed
Tt is Yised when the Index of subscript
in aseaiena t out Dt Tange.
YExplatn Taise and assert statemet with Syntar anel
-Raise s tatenent - The rase stotement lan be used Crample
to thow an
Auption by uing the tououw tng syntak
Syntax: Tas trceptton -namt (loponal anguments)
EX Tai'se eKcep h'on ('oops| An excapion has Occurred)
Assert s t o t e m e n t An asset statement {s used to test an eapressto
[n tht proqom Cou. tne vesS ult after testing ts talse then
th exLe ton'is rot'sed. Tnis statement is alty used n the
of the fnction or after the ton cell.
bqinning
Syntax assert expregston | argments)
nmbtr
det
negatiue
hece cn)
it's
assert (n= O) oops a
nqatve
prC n « n )
prínt (Chede cio4t (o0
negatve num bey
3] EAplain Anthe ProLes of Excaptton Htardling
eTror encounteed
to a method
A
Create eKupbon obie
hong
hoing an fxcephon
An Exuptton is raised
Runtime System searches for
oundexCep tion handler ina lurrent
No founo
method
Bearhes for methods in
Exeutes the Codek found Call stack tn everse Seance
[Cathinq Preqrom terminates]
A fxception Handlírg- toEath and evny exapton neod to be handled
avo ld abneemal teiminat'on of proqram.
omme
by the pa0g4 wríting on additon (ode in a progrom to ive
Th& dendon byby writng
proper mesSsadk or nstruction to the Usr
Ilirowíng Exuphon untime
na síng to the
-The procras of crea hng eheeptíon objc and
System s lcnowt
exeptons aTe Caught tn the
(atthíng onExcephon Te ony be handled tn the ex cept block
block then it should
a suitable handler s
The process of executng
tatching on Exceptío
blok white
-tKteptmiqht doubt an txcepton to
a parhula
part ot the code such Susprous (nes put
except bloce
bloule s folloued by an
tnyblock. Eve y tny block
Cose I: sng Single tg- except
nuneYa tor 50
tnput(" enter the val w ))
denom ; (nt
Quottent = (numerator| denom)
prúnt (auotént)
print(" Diviston pertomed nomally")
except ero Díviseon Erro: be zeo") not
(Denomnator
should
prnt blok)
try-txcept
print ("Out of
else clause
- else lause.- We Can have an op hional
Ty-txcept block. An txcupt block
will be
usith ty -tKupt
a xeusted only 't are taised but f
't Sorme enceptions
the exupt
there is of CXcption then ene ot
statements
wtll be enecuttd n thís Case, the
block
block will be executed
Inside the e lse
Ex : Use ot else clause
numera tor :so
de nor int ( input (" Enter the Value"))
Quotent -(numeratoy denom)
prínt (auohtnt).
pint DÊv(aon pe formed normally)
txcapt not be z(o")
pint"DenomínbY Should
Cxept Valus Crror are allowed")
operatíon,ts
prínt ("auotent of) thu
Auotent)
Fínally clauie
A try Statenent in python can have en aphional tnally
blok are alays
block The statemens faride the finallu has Durecl in
exettd regardle« of an tKtaphon tÏ use frhally blok
(s lommen 'praLe
tny-blok or'not. It ís
Dt aluny placs
tle objet is closrd.
to ensure that thw e xeupt bleu and the
at the end ot ty- lou aqter all
else bloLk
denom n t (íoput ("Enter the valw")
&uotienb =numerator denom)
pint (auctient)
Djvírion perfomed nomaly")
prínt ("erO divínon tTDr;
trcupt Denominator Should not b zemy
print (
Value Emor:
exupt Pnt("only integers are allouotd)
else.
prínt uotent ot the operctton
Ôuotent )
finall y
pnnt ("over and ou")
Stak
Stack is empty: -!
isE mpty Push [NotrequiredJ
is Fuu Pop [ Not requiredJ