Program to raise a user defined exception if username is less than 6 characters and password does not match
Java exception handling is managed via five keywords: try, catch, throw, throws and finally.
- try - Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown.
- catch - Your code can catch this exception (using catch) and handle it in some rational manner.
- throw - System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw.
- throws - Any exception that is thrown out of a method must be specified as such by a throws clause.
- finally - Any code that absolutely must be executed before a method returns is put in a finally block.
The syntax of Exception Handling block is as follows:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// ...
finally {
// block of code to be executed before try block ends
}
The following program generates an exception if the user types wrong username or password. Here, we have created two exception classes UsernameException and PasswordException which extends the Exception class. The statements which throws an exception we put them in the try block and handle this exception in catch block. throw keyword is used to throw an exception explicitly. The outputs for different situations are shown after program.
PROGRAM
import java.util.Scanner;
class UsernameException extends Exception {
public UsernameException(String msg) {
super(msg);
}
}
class PasswordException extends Exception {
public PasswordException(String msg) {
super(msg);
}
}
public class CheckLoginCredential {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String username, password;
System.out.print("Enter username :: ");
username = s.nextLine();
System.out.print("Enter password :: ");
password = s.nextLine();
int length = username.length();
try {
if(length < 6)
throw new UsernameException("Username must be greater than 6 characters ???");
else if(!password.equals("hello"))
throw new PasswordException("Incorrect password\nType correct password ???");
else
System.out.println("Login Successful !!!");
}
catch (UsernameException u) {
u.printStackTrace();
}
catch (PasswordException p) {
p.printStackTrace();
}
finally {
System.out.println("The finally statement is executed");
}
}
}
OUTPUT 1
C:\>javac CheckLoginCredential.java C:\>java CheckLoginCredential Enter username :: Raj Enter password :: hello exception.UsernameException: Username must be greater than 6 characters ??? at exception.CheckLoginCredential.main(CheckLoginCredential.java:41) The finally statement is executedOUTPUT 2
C:\>javac CheckLoginCredential.java C:\>java CheckLoginCredential Enter username :: Sangram Enter password :: 123 exception.PasswordException: Incorrect password Type correct password ??? at exception.CheckLoginCredential.main(CheckLoginCredential.java:43) The finally statement is executedOUTPUT 3
C:\>javac CheckLoginCredential.java C:\>java CheckLoginCredential Enter username :: Sangram Enter password :: hello Login Successful !!! The finally statement is executed