Chapter 14 ps

 package com.company;

import java.util.Scanner;
class maxRetriesReachException extends Exception {
@Override
public String toString() {
return "Maximum try reached! stop!";
}

@Override
public String getMessage() {
return "Stop! Stop!";
}

}

public class CWH_Ch14_Ps {
public static void meth() throws IndexOutOfBoundsException {
int[] marks = {4, 5, 7, 6, 6, 6, 6, 6, 9};
boolean b1 = true;
int a = 1;
Scanner sc1 = new Scanner(System.in);
while (b1) {
if (a < 6) {
System.out.println("Enter the value of index");
int i = sc1.nextInt();
try {
System.out.println(marks[i]);
System.out.println("Right index");
b1 = false;
} catch (Exception e) {
System.out.println("Index out of bound");
} finally {
a++;
}
} else {
System.out.println("Enter the value of index");
int i = sc1.nextInt();
try {
throw new maxRetriesReachException();
} catch (maxRetriesReachException e) {
System.out.println(e.getMessage());
System.out.println(e.toString());
}
break;
}
}
}
// public static int divide(int x) {
// int a = 1000;
// int c = a / x;
// return c;
//}
public static void main(String[] args) {
// problem 1

// Syntax error

//int i=9 // ---> syntax error

// Logical error--> print all odd numbers
// for (int i =1;i < 12;i++) {
// System.out.println(2 * i);// giving all even numbers upto 11
// }
// Runtime error
// int a = 1;
// int b = 0;
// int c = a / b;

// Problem 2
// Scanner sc = new Scanner(System.in);
// System.out.println("Enter value of b");
// int b = sc.nextInt();
// try {
// System.out.println(divide(b));
// } catch (ArithmeticException e) {
// System.out.println("Haha");
// } catch (InputMismatchException e) {
// System.out.println("Hehe");
// }
// Problem 3
// problem 4
meth();
}
}

Comments