Java Programming Practical 12

Question 1
Develop a program to accept a password from the user and throw “Authentication Failure” Exception if the password is incorrect.
ExceptionDemo.java
Java
				import java.util.Scanner;

class MyException extends Exception 
{
    public MyException(String message) 
	{
        super(message);
    }
}

public class ExceptionDemo 
{
    public static void main(String[] args) 
	{
        Scanner scanner = new Scanner(System.in);

        try {
            System.out.print("Enter your password: ");
            String pass = scanner.nextLine();

            if (!pass.equals("Admin")) 
			{
                throw new MyException("Authentication Failure: Incorrect password provided.");
            }

            System.out.println("Authentication successful! Welcome.");

        } 
		catch (MyException e) 
		{
            System.out.println(e.getMessage());
        } 
    }
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *