Friday, 3 June 2016

User Defined Exception in Java


                        User Defined Exception in Java


package Excep;

import java.util.Scanner;

public class User_Exception {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter Your Age in Number");

int age = scanner.nextInt();

try {

if (age < 0) {
           
//  Anonymous inner class
throw new ArithmeticException() {

private static final long serialVersionUID = -1962716995912160014L;

@Override
public String toString() {

return "Age can not be negative";
}
};

} else {
System.out.println("Age is ok");
}
} catch (Exception e) {

System.out.println("catch block executed");
e.printStackTrace();

}

finally {

System.out.println("finally block executed");
}

}

}

Thanks
Keep Learning.......... :)

No comments:

Post a Comment