package patterns1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class ExceptionDemo {
public static void main(String[] args) {
ExceptionDemo ed = new ExceptionDemo();
//ed.divide1();
//ed.divide2();
//ed.divide3();
//ed.checkedExceptionDemo();
try {
ed.getPassword();
} catch (PwdException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
private void getPassword() throws PwdException {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("enter password");
String password = sc.next();
if(password.length() <8)
{
//PwdException pwde = new PwdException("Less than 8 Exception");
//throw pwde;
throw new PwdException("Less than 8 Exception"); //Anonymous Object
}
}
private void checkedExceptionDemo() {
// TODO Auto-generated method stub
//Scanner sc = new Scanner(System.in);
// String fileName = sc.next();//c://new.txt
// File file = new File(fileName);
/*try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} */
File file = new File("abcd.txt");
try {
FileReader fr = new FileReader(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("Please check file location");
}
}
private void divide3() {
// TODO Auto-generated method stub
int[] ar = new int[5];
try {
//login - username, password
// 10 minutes
//System.out.println("hi");
System.out.println(10/0);
System.out.println("hi");
}
catch(Exception e)
{
//e.printStackTrace();
//System.out.println(e.getMessage());
System.out.println("Check Check");
}
finally
{
System.out.println("finally block");
}
System.out.println("Proceed further");
}
private void divide2() {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
try
{ // Exception Possible area
System.out.println("Enter two no.s ");
int no1 = sc.nextInt();
int no2 = sc.nextInt();
System.out.println(no1/no2);
try {
int[] ar = new int[no1];
System.out.println(ar[0]);
}
catch(NegativeArraySizeException ne)
{
System.out.println("Please check array length");
}
}
catch(Exception e)
{
System.out.println("Something went wrong");
}
System.out.println("Hi");
}
private void divide1() {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
try { // Exception Possible area
System.out.println("Enter two no.s ");
int no1 = sc.nextInt();
int no2 = sc.nextInt();
System.out.println(no1/no2);
int[] ar = new int[no1];
System.out.println(ar[0]);
}
catch(ArithmeticException ae)
{ // Exception Handling area
System.out.println("Please check no2 ");
//divide();
}
catch(InputMismatchException ime)//two
{
System.out.println("Check inputs ");
}
catch(Exception e)//-7 , 2
{
System.out.println("Something went wrong");
}
System.out.println("Hi");
}
}
Outputs:
getPassword:
enter password
1234
Less than 8 Exception
checkedExceptionDemo:
Please check file location
divide3:
Check Check
finally block
Proceed further
divide2:
1
-6
0
0
Hi
divide1:
Enter two no.s
1
two
Check inputs
Hi
package patterns1;
public class PwdException extends Exception {
//If this class is sub class RuntimeException - UnCheckedException
//Otherwise, it is Checked Exception
public PwdException(String message)
{
System.out.println(message);
}
}
Leave a comment