0% found this document useful (0 votes)
20 views2 pages

Exception

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

Exception

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

What is exception handling?

Ans:> Exception is nothing but runtime errors and it occurs due


to incorrect implementation of logic.

Real life example:

Money transfer,

ex: a/b
where b=0
will get exception,and all code
after the line will get stopped, mns they are not running.
for that we need to handle the exception to resume the flow of program

#exception handling

mechanism which help us to handle runtime errors.

#Process of exception Handling:


Try,except,else

Try: we have to put the risky code inside the try block.

if there is any chances of expection then the except block will be executed
else the try block itself execute the code.

a= int(input("enter first no"))


b=int(input("enter 2nd no"))

res=a/b
print("final result",res)

print()

---------------To handle above exception(put risky code inside try)---------


a= int(input("enter first no"))
b=int(input("enter 2nd no"))
try:
res=a/b
print("final result",res)

except:
print("cannot devide to zero")
print("hello")
----------------------------------Else block----
if everything inside the try block is correct then always the else block will be
executed.

a= int(input("enter first no"))


b=int(input("enter 2nd no"))
try:
res=a/b
print("final result",res)

except:
print("cannot devide to zero")
else:
print("hello")
------------------Assert:
Python provides the assert statement to check if a given logical
exp is true or false.
prog execution proceeds only if the expression is true and raise
the AssertionError when it is false.

eg:
num=15
assert num>=15

try:
num=int(input("entr a no"))
assert num%2==0
print("Number is even")
except AssertionError:
print("pls enter even no")

You might also like