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

Practical No 16

The program first demonstrates how to handle a ZeroDivisionError exception by dividing user-inputted numerator and denominator values. If the denominator is 0, it prints an error message. It then shows how to define a custom IncorrectPasswordError exception class and raise it if the user-inputted password does not match the correct password, printing an error message if the exception is raised.
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)
51 views2 pages

Practical No 16

The program first demonstrates how to handle a ZeroDivisionError exception by dividing user-inputted numerator and denominator values. If the denominator is 0, it prints an error message. It then shows how to define a custom IncorrectPasswordError exception class and raise it if the user-inputted password does not match the correct password, printing an error message if the exception is raised.
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

# write a python program to check for ZeroDivisionError Exception.

try:

numerator = int(input("Enter the numerator: "))

denominator = int(input("Enter the denominator: "))

result = numerator / denominator

print("Result:", result)

except ZeroDivisionError:

print("Error: Cannot divide by zero.")

except ValueError:

print("Error: Please enter valid integers.")

# Write a python program to create user defined exception that will check whether the password is correct or not?

class IncorrectPasswordError(Exception):

pass

def authenticate(password):

correct_password = "correct_password"

if password != correct_password:

raise IncorrectPasswordError("Incorrect password")

try:

password_attempt = input("Enter your password: ")

authenticate(password_attempt)

print("Login successful!")

except IncorrectPasswordError as e:

print(e)

You might also like