Write a Java program to accept a password from the user and throw "Authentication Failure" exception if the password is incorrect.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class AuthenticationException extends Exception {
public AuthenticationException(String message) {
super(message);
}
}
public class AuthenticationExcDemo {
public static void main(String[] args) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String pwd;
try {
System.out.print("Enter password :: ");
pwd = br.readLine();
if(!pwd.equals("123"))
throw new AuthenticationException("Incorrect password\nType correct password");
else
System.out.println("Welcome User !!!");
}
catch (IOException e) {
e.printStackTrace();
}
catch (AuthenticationException a) {
a.printStackTrace();
}
System.out.println("BYE BYE");
}
}
Enter password :: 123
Welcome User !!!
BYE BYE
Enter password :: abc
exception.AuthenticationException: Incorrect password
Type correct passwordBYE BYE
at exception.AuthenticationExcDemo.main(AuthenticationExcDemo.java:34)