0% found this document useful (0 votes)
12 views3 pages

Prasad Exception Handling New

The document provides examples of exception handling in Python, demonstrating both scenarios where exceptions can and cannot occur. It includes code snippets for handling division by zero and index errors, as well as a user-defined exception for invalid age input. The outputs of each example illustrate the results of the exception handling processes.

Uploaded by

wafflerick69
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)
12 views3 pages

Prasad Exception Handling New

The document provides examples of exception handling in Python, demonstrating both scenarios where exceptions can and cannot occur. It includes code snippets for handling division by zero and index errors, as well as a user-defined exception for invalid age input. The outputs of each example illustrate the results of the exception handling processes.

Uploaded by

wafflerick69
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/ 3

Name :- Prasad Mahesh Chaudhari

Class :- SY – IT Roll no. :- 10 Prac :- 06

-------------------------------------------------------------------------------------------------------

#Exeption Handling.

#Exception Cannot Occurs

try:

numarator=10

denominator=20

result=numarator/denominator

print(result)

except:

print("Error : denominator cannot be 0.")

#Output: - 0.5

---------------------------------------------------------------------------------------------------------------------------------------
#Exception Can Occurs

try: #

numarator=10

denominator=0

result=numarator/denominator

print(result)

except:

print("Error : denominator cannot be 0.")

#Output:- Error : denominator cannot be 0.

----------------------------------------------------------------------------------------------------------------------------------------

# Inbuilt Exception
try:

even_numbers = [2,4,6,8]

print(even_numbers[5])

except ZeroDivisionError:

print("denominator can not be 0.")

except IndexError:

print("Index out of bound.")

Output:- Index out of bound.

# ZeroDivisionError and IndexError :- It is a class which is inherited from Exception class


#User Define Exception

class InvalidAgeException(Exception):

"Raised when the input value is less than 18"

pass

number = 18

try:

input_num = int(input("Enter an Age: "))

if input_num < number:

raise InvalidAgeException

else:

print("Eligible to vote")

except InvalidAgeException:

print("Exception Occurred : Invalid Age")

Output :-

Enter an Age: 4

Exception Occurred : Invalid Age

Enter an Age: 20

Eligible to vote

You might also like