0% found this document useful (0 votes)
2 views7 pages

Exception Handling

The document discusses exception handling in Python, explaining how to catch and manage exceptions using try and except blocks. It emphasizes the importance of handling exceptions to prevent program crashes and provides examples of raising exceptions and using finally blocks. Additionally, it covers the syntax for handling exceptions and the execution flow when exceptions occur.

Uploaded by

BANU
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 views7 pages

Exception Handling

The document discusses exception handling in Python, explaining how to catch and manage exceptions using try and except blocks. It emphasizes the importance of handling exceptions to prevent program crashes and provides examples of raising exceptions and using finally blocks. Additionally, it covers the syntax for handling exceptions and the execution flow when exceptions occur.

Uploaded by

BANU
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/ 7

BE CSE-B

40
n Pyhon eACepAion handing using
)Give a bie note finally stalements
ty, exCepty vaix and
Handling Exceptions the current process
occux, it ma kes it us
exception pxocess until
*bhen the calling
to
stop andfuys it paoqam will cash
to ledy the un
.If not hand
funchion B which
handied calls tunctionC.
l! function A s
e xampley Occurs
* FON ec Ception panes to B
functionC, an escception
Eusncalis handled C the
u C,
not spitout
IfIt us
the ko A error messa qe
an d
handle d an uhexpected halE:
IÊ neves a sudden,
proqvam comes to
andthe using a
in python handled
Catching Exceptions excepticns Can be
Inpython, executing the bxy claue
-It Staxt s by Called
Exy' statement 3tatemen t s
ercepticn txq
cwith a ty
Handlingan escception'
Catchinq an
Syntax:
Statements
except &ception Name:
Sta tements
Statements between the tvs and
* First the ts bloc k
eccept ae ecceuted.
the except block us 3kipped
* If nD e>eception occuxsy
* I} an xceptin Oaus, duing exeaution o4 any
block
St atment tin thcEay bloclk Hcos the vest of the Exy

matches the exception named


oIf the exception type executed
that except block s
afte thê except keyyoxdy Stotemen t
theu
andthen execulion tontinues aflex
I{ the exception kupe does not matches the exception
0
keytooxd then t san unhandled
named aftes the except
Eexminated oith an exrOY
exception and the proqram us
message
Example : Finding veiprocal of
Import Sy 3
N= Da, o, 2]
fox x n N:
Exy:
Pint ("The input is", x)
T= /int(c)
bxeak
except:
ss3.e>ec "xcused!)
-info)oJ,
Pint ("oops!,
Print ("Nextinput")
Pint ()
hint ("The xecipsocal of'yx$ is",x)
cuiput:
The input u a
cop s cClaus' value Error oCiuMed.
Raiwing Exerion
the exception can be delibeatels vased
wing the ut keyuod.
Syntax:
xise [EXcepion C,axgs Cytxacebac k]] I
Hexe, &xception us the type of exception and
agsuments çs a value for the exception axqument Ihe
jaxqument ç opAionaly if not Supplied, the exception
axqumeht s None

Pogram
Exy:
num= I00

Psint Cnum)
xa·e Value Exxox
ercept:
Occused- Progxam Terninating")
Psint ("Exeption
Output:
IDD
CCtued. Proqram Texminating
Exception
The Finalls Block
an optional block Called
* The t xy block has
a past of ecception handling
finallu- It à aso befoxe
fnally block us always executed
* The
bxy bloc k It us always used for doing the
leaving +he
Python. Nethexlond.level
Connection Proqramminq high
a cception sing
thwlexecution
ssouves exeauted) U new in Scalable,
Rossum
base e
e zero zexo") a Python
file clata by always cutedexe
înto Von oriented
closing Progam
elosing divide Exror:
Zesobiyvision
except
by files and Guico
divide is
eso.
xs Z multiple 3olving object
lac TnCs, the ("Can't by aluays
vis
This by
Th
tasks endinmessage
delightfud CK) Psint (Print divide mesqe Problemdeveleped ble,
language
intexpreted
5//o Psint posta
e Finally:
cluding
Systonbe k= Con't
o
may
:Evy
Phogvam: DUEput: Eo
Houo
3) :files FilewasExt
Filetwelcome
Datal·tX txt 3-Filea
con the Puthen uson
Y th
Du
LxE
3'E:file programmible,Scalang zeyc
veddc) un )
osienked
Rossurn
txt) by
infile:
filedoutfile: file
filenames:. thon
division
tc:"))
Van object n:"))d:;))
': )as CinOukfile-oikePy
as (CGname and povtable,the
Guido lanquage D! value
of o
)
txt 'w) Solving handle Valulue
va
zexo
by )
ilel un , open
C'E ile' developed the the the Erpx
C"Entes ",("GBuotient:
:
fname to by
PE: oith prOblem ua tnteypreted
proqam Enter
("EnAer sion
Pivision
python ("Divi
filenames
open foy Cinput
(Cinput
oith File Eo
txt
ouBput: weltome
was
landlevel a eception(input PrintZexoPint
|Boqnam +hon |highWe Proqvan:
Nethex n=inteint eint eccept
Py 3)
d e
n:lbd:5C:5
ot by
0f o4 valuezerO|
Divi'sion
Valuevalue
the the the
Cut EntaEnterEntey
put:

You might also like