1public static void main(String[] args) {
2 Scanner kb = new Scanner(System.in);
3 System.out.println("Enter a number");
4 try {
5 double nb1 = kb.nextDouble();
6 if(nb1<0)
7 throw new ArithmeticException();
8 else System.out.println( "result : " + Math.sqrt(nb1) );
9 } catch (ArithmeticException e) {
10 System.out.println("You tried an impossible sqrt");
11 }
12}
1/* In this program we are checking the Student age
2 * if the student age<12 and weight <40 then our program
3 * should return that the student is not eligible for registration.
4 */
5public class ThrowExample {
6 static void checkEligibilty(int stuage, int stuweight){
7 if(stuage<12 && stuweight<40) {
8 throw new ArithmeticException("Student is not eligible for registration");
9 }
10 else {
11 System.out.println("Student Entry is Valid!!");
12 }
13 }
14
15 public static void main(String args[]){
16 System.out.println("Welcome to the Registration process!!");
17 checkEligibilty(10, 39);
18 System.out.println("Have a nice day..");
19 }
20}
1void testMethod() throws ArithmeticException, ArrayIndexOutOfBoundsException {
2 // rest of code
3}