Page 290 - CTS - CSA TP - Volume 2
P. 290

COMPUTER SOFTWARE APPLICATION - CITS




           TASK 3: Custom Exception
                 # Example 3: Custom Exception
                 classCustomError(Exception):
                  pass

                 try:
                 user_input=input(“Enter ‘raise’ to simulate a custom exception: “)
                 ifuser_input.lower() ==’raise’:
                 raiseCustomError(“This is a custom exception.”)

                 exceptCustomErrorase:
                 print(f”CustomError caught: {e}”)
                 else:
                 print(“No exception raised.”)
                 finally:
                 print(“Exception handling completed.”)

           Explanation:
           •  We define a custom exception CustomError.
           •  If the user enters ‘raise’, we deliberately raise the custom exception.
           •  exceptCustomError as e catches the custom exception and prints the error message.

           •  else block executes if no exception is raised.
           •  finally block always executes.
           Output:

























           TASK 4: User Authentication
                 correct_username=”user123”

                 correct_password=”password123”
                 try:
                 username=input(“Enter your username: “)
                 password=input(“Enter your password: “)





                                                           275
 CITS : IT & ITES - Computer Software Application - Exercise 131  CITS : IT & ITES - Computer Software Application - Exercise 131
   285   286   287   288   289   290   291   292   293   294   295