w3ajay

tally tutorial point, ms word 2013, ms excel 2013, ms powerpoint 2010,ccc question with answer in hindi 2021, Tally Prime in hindi , tally prime,Python,in python,programming in python,python

Tuesday, August 27, 2024

what is keyword of exception in python in class 12

 he exception keyword in Python


What is keyword of exception in python in class 12

Exception have four Keyword 

i) try:
ii) except:
iii) else:
iv) finally:

try block:

try block is the block that contains the code of segment that might contain exception 

except:

When exception is found in try block. the code inside exception block is executed handle exception .one try block can have many except block 

Program 1:

try:
    print(a)
except:
    print("an exception occured")

Output

an exception occured

Program 2:

try:
    x=5
    print(x)
    c=x/0
    print(c)
except NameError:
    print("Variable Not found")
except ZeroDivisionError:
    print("Divide By Zero Error")
except:
    print("Some Error Happend.............")

5
Divide By Zero Error

else block:

if no error found in try block then else block is executed

finally block:

it is a block in which it dose not matter whether the try block error or not

 

Program 3:

try:
    print("Hello we are learning exception ")
except:
    print("Some Error found")
else:
    print("No Error Found")
finally:
    print("Hello Friend it is my Final Program.............")

Output

Hello we are learning exception 
No Error Found
Hello Friend it is my Final Program.............

Program 4:

try:
    f=open("ram.txt","w")
    print("file open successfully")
except:
    print("The file does not exit......")
finally:
    f.close()


raise Exception:

it is used to create user defined exception 

Program 5:


x=-100
if x<0:
    raise Exception("The Number is less than Zero")

Output

Traceback (most recent call last):
  File "C:/Users/Ajay Patel/Desktop/allprogram.py", line 4, in <module>
    raise Exception("The Number is less than Zero")
Exception: The Number is less than Zero

Program 6:

x="w3ajay"
if not type(x) is int:
    raise Exception("Only Integer Allowed")

Output

Traceback (most recent call last):
  File "C:/Users/Ajay Patel/Desktop/allprogram.py", line 3, in <module>
    raise Exception("Only Integer Allowed")
Exception: Only Integer Allowed

No comments:

Post a Comment

for more information please share like comment and subscribe