├── bin └── com │ └── w3epic │ └── wiprotraining │ ├── assignment1 │ └── Assignment1.class │ ├── assignment2 │ └── Assignment2.class │ ├── assignment3 │ └── Assignment3.class │ ├── assignment4 │ └── Assignment4.class │ ├── assignment5 │ └── Assignment5.class │ ├── assignment6 │ ├── Assignment6.class │ ├── NegativeValuesException.class │ └── ValuesOutOfRangeException.class │ ├── assignment8 │ ├── Assignment8.class │ └── InvalidAgeException.class │ ├── assignment9 │ └── Assignment9.class │ └── assignment7 │ ├── UserRegistration.class │ └── InvalidCountryException.class ├── src └── com │ └── w3epic │ └── wiprotraining │ ├── assignment8 │ ├── InvalidAgeException.java │ └── Assignment8.java │ ├── assignment6 │ ├── NegativeValuesException.java │ ├── ValuesOutOfRangeException.java │ └── Assignment6.java │ ├── assignment7 │ ├── InvalidCountryException.java │ └── UserRegistration.java │ ├── assignment5 │ └── Assignment5.java │ ├── assignment1 │ └── Assignment1.java │ ├── assignment9 │ └── Assignment9.java │ ├── assignment4 │ └── Assignment4.java │ ├── assignment2 │ └── Assignment2.java │ └── assignment3 │ └── Assignment3.java └── README.md /bin/com/w3epic/wiprotraining/assignment1/Assignment1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment1/Assignment1.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment2/Assignment2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment2/Assignment2.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment3/Assignment3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment3/Assignment3.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment4/Assignment4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment4/Assignment4.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment5/Assignment5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment5/Assignment5.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment6/Assignment6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment6/Assignment6.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment8/Assignment8.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment8/Assignment8.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment9/Assignment9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment9/Assignment9.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment7/UserRegistration.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment7/UserRegistration.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment8/InvalidAgeException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment8/InvalidAgeException.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment6/NegativeValuesException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment6/NegativeValuesException.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment7/InvalidCountryException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment7/InvalidCountryException.class -------------------------------------------------------------------------------- /bin/com/w3epic/wiprotraining/assignment6/ValuesOutOfRangeException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Cyberster/Wipro-Training-Abstraction-Packages-Exception-Handling---Assignments-for-Exception-Handling/HEAD/bin/com/w3epic/wiprotraining/assignment6/ValuesOutOfRangeException.class -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment8/InvalidAgeException.java: -------------------------------------------------------------------------------- 1 | package com.w3epic.wiprotraining.assignment8; 2 | 3 | public class InvalidAgeException extends Exception { 4 | public InvalidAgeException() { 5 | super(); 6 | System.out.println("Invalid age"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment6/NegativeValuesException.java: -------------------------------------------------------------------------------- 1 | package com.w3epic.wiprotraining.assignment6; 2 | 3 | public class NegativeValuesException extends Exception { 4 | public NegativeValuesException() { 5 | super(); 6 | System.out.println("NegativeValuesException occured"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment6/ValuesOutOfRangeException.java: -------------------------------------------------------------------------------- 1 | package com.w3epic.wiprotraining.assignment6; 2 | 3 | public class ValuesOutOfRangeException extends Exception { 4 | public ValuesOutOfRangeException() { 5 | super(); 6 | System.out.println("ValuesOutOfRangeException occured"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment7/InvalidCountryException.java: -------------------------------------------------------------------------------- 1 | package com.w3epic.wiprotraining.assignment7; 2 | 3 | public class InvalidCountryException extends Exception { 4 | public InvalidCountryException() { 5 | super(); 6 | System.out.println("InvalidCountryException occured"); 7 | System.out.println("User Outside India cannot be registered"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment8/Assignment8.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program to accept name and age of a person from the command prompt(passed as 3 | arguments when you execute the class) and ensure that the age entered is >=18 and < 60. 4 | Display proper error messages. 5 | The program must exit gracefully after displaying the error message in case the arguments 6 | passed are not proper. (Hint : Create a user defined exception class for handling errors.) 7 | * */ 8 | 9 | package com.w3epic.wiprotraining.assignment8; 10 | 11 | public class Assignment8 { 12 | 13 | public static void main(String[] args) throws InvalidAgeException { 14 | String name = args[0]; 15 | 16 | int age = Integer.parseInt(args[1]); 17 | 18 | if (age < 18 || age >= 60) 19 | throw new InvalidAgeException(); 20 | 21 | System.out.println("Name: " + name + " Age: " + age); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment5/Assignment5.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a Program with a division method who receives two integer numbers and performs the 3 | division operation. The method should declare that it throws ArithmeticException. 4 | This exception should be handled in the main method. 5 | * */ 6 | 7 | package com.w3epic.wiprotraining.assignment5; 8 | 9 | import java.util.Scanner; 10 | 11 | public class Assignment5 { 12 | 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | 16 | int a = sc.nextInt(); 17 | int b = sc.nextInt(); 18 | 19 | try { 20 | double r = division(a, b); 21 | System.out.println(r); 22 | } catch (ArithmeticException e) { 23 | System.out.println(e.getMessage()); 24 | } 25 | 26 | sc.close(); 27 | } 28 | 29 | public static double division(int a, int b) throws ArithmeticException { 30 | return a / b; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment1/Assignment1.java: -------------------------------------------------------------------------------- 1 | /* 2 | Handle exception in number 3 | Problem statement: 4 | Get the input String from user and parse it to integer, if it is not a number it will throw 5 | number format exception Catch it and print "Entered input is not a valid format for an integer." 6 | or else print the square of that number. (Refer Sample Input and Output). 7 | Sample input and output 1: 8 | Enter an integer: 12 9 | The square value is 144 10 | The work has been done successfully 11 | Sample input and output 2: 12 | Enter an integer: Java 13 | Entered input is not a valid format for an integer. 14 | * */ 15 | 16 | package com.w3epic.wiprotraining.assignment1; 17 | 18 | import java.util.Scanner; 19 | 20 | public class Assignment1 { 21 | 22 | public static void main(String[] args) { 23 | Scanner sc = new Scanner(System.in); 24 | 25 | System.out.print("Enter an integer: "); 26 | String str = sc.nextLine(); 27 | 28 | try { 29 | int x = Integer.parseInt(str); 30 | System.out.println("The square value is: " + x * x); 31 | System.out.println("The work has been done successfully"); 32 | } catch (NumberFormatException e) { 33 | System.out.println("Entered input is not a valid format for an integer."); 34 | //e.printStackTrace(); 35 | } 36 | 37 | sc.close(); 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment9/Assignment9.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that accepts 2 integers a and b as input and finds the quotient of a/b. 3 | This program may generate an Arithmetic Exception. Use exception handling mechanisms to 4 | handle this exception. In the catch block, print the message as shown in the sample output. 5 | Also illustrate the use of finally block. Print the message “Inside finally block”. 6 | Sample Input and Output 1: 7 | Enter the 2 numbers 8 | 5 9 | 2 10 | The quotient of 5/2 = 2 11 | Inside finally block 12 | Sample Input and Output 2: 13 | Enter the 2 numbers 14 | 5 15 | 0 16 | DivideByZeroException caught 17 | Inside finally block 18 | * */ 19 | 20 | package com.w3epic.wiprotraining.assignment9; 21 | 22 | import java.util.Scanner; 23 | 24 | public class Assignment9 { 25 | 26 | public static void main(String[] args) { 27 | Scanner sc = new Scanner(System.in); 28 | 29 | try { 30 | System.out.println("Enter the 2 numbers"); 31 | int a = sc.nextInt(); 32 | int b = sc.nextInt(); 33 | int quotient = a / b; 34 | System.out.println("The quotient of " + a + " / " + b + " = " + quotient); 35 | } catch (ArithmeticException e) { 36 | System.out.println(e.getMessage() + " caught"); 37 | } finally { 38 | System.out.println("Inside finally block"); 39 | } 40 | 41 | sc.close(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment4/Assignment4.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a class MathOperation which accepts integers from command line. 3 | Create an array using these parameters. Loop through the array and obtain the sum and 4 | average of all the elements. 5 | Display the result. 6 | Check for various exceptions that may arise like ArithmeticException, NumberFormatException, and so on. 7 | For example: The class would be invoked as follows: 8 | C:>java MathOperation 1900, 4560, 0, 32500 9 | 10 | * */ 11 | 12 | package com.w3epic.wiprotraining.assignment4; 13 | 14 | import java.util.InputMismatchException; 15 | 16 | public class Assignment4 { 17 | 18 | public static void main(String[] args) { 19 | int n = args.length; 20 | 21 | for (int i = 0; i < n; i++) 22 | if (args[i].charAt(args[i].length() - 1) == ',') 23 | args[i] = args[i].replace(",", ""); 24 | 25 | //System.out.println(Arrays.toString(args)); 26 | 27 | int[] arr = new int[n]; 28 | 29 | int sum = 0; 30 | double avg = 0; 31 | 32 | try { 33 | for (int i = 0; i < n; i++) 34 | arr[i] = Integer.parseInt(args[i]); 35 | 36 | for (int i = 0; i < n; i++) 37 | sum += arr[i]; 38 | 39 | avg = sum / n; 40 | } catch (NumberFormatException e) { 41 | System.out.println("NumberFormatException"); 42 | } catch (ArithmeticException e) { 43 | System.out.println("ArithmeticException"); 44 | } catch (InputMismatchException e) { 45 | System.out.println("InputMismatchException"); 46 | } 47 | 48 | System.out.println("sum: " + sum); 49 | System.out.println("avg: " + avg); 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment7/UserRegistration.java: -------------------------------------------------------------------------------- 1 | /* 2 | A student portal provides user to register their profile. During registration the system needs 3 | to validate the user should be located in India. If not the system should throw an exception. 4 | Step 1: Create a user defined exception class named “InvalidCountryException”. 5 | Step 2: Overload the respective constructors. 6 | Step 3: Create a main class “UserRegistration”, add the following method, 7 | registerUser– The parameter are String username,String userCountry and add the following logic, 8 | • if userCountry is not equal to “India” throw a InvalidCountryException with the message 9 | “User Outside India cannot be registered” 10 | • if userCountry is equal to “India”, print the message “User registration done successfully” 11 | Invoke the method registerUser from the main method with the data specified and see how the program behaves, 12 | Name Country Expected Output 13 | Mickey US InvalidCountryException should be thrown. 14 | The message should be “User Outside India cannot be registered” 15 | Mini India The message should be “User registration done successfully” 16 | Sample Input and Output 17 | * */ 18 | 19 | package com.w3epic.wiprotraining.assignment7; 20 | 21 | public class UserRegistration { 22 | 23 | public void registerUser(String username, String userCountry) throws InvalidCountryException { 24 | if (!userCountry.equals("India")) 25 | throw new InvalidCountryException(); 26 | else 27 | System.out.println("User registration done successfully"); 28 | 29 | 30 | } 31 | 32 | public static void main(String[] args) { 33 | // TODO Auto-generated method stub 34 | UserRegistration registration = new UserRegistration(); 35 | 36 | try { 37 | registration.registerUser("Mickey", "US"); 38 | //registration.registerUser("Mini", "India"); 39 | } catch (InvalidCountryException e) { 40 | //System.out.println(e.getMessage()); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment2/Assignment2.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes as input the size of the array and the elements in the array. 3 | The program then asks the user to enter a particular index and prints the element at that index. 4 | This program may generate Array Index Out Of Bounds Exception. Use exception handling mechanisms 5 | to handle this exception. In the catch block, print the class name of the exception thrown. 6 | 7 | Sample Input and Output 1: 8 | Enter the number of elements in the array 9 | 3 10 | Enter the elements in the array 11 | 20 12 | 90 13 | 4 14 | Enter the index of the array element you want to access 15 | 2 16 | The array element at index 2 = 4 17 | The array element successfully accessed 18 | 19 | Sample Input and Output 2: 20 | Enter the number of elements in the array 21 | 3 22 | Enter the elements in the array 23 | 20 24 | 90 25 | 4 26 | Enter the index of the array element you want to access 27 | 6 28 | java.lang.ArrayIndexOutOfBoundsException 29 | * */ 30 | 31 | package com.w3epic.wiprotraining.assignment2; 32 | 33 | import java.util.Scanner; 34 | 35 | public class Assignment2 { 36 | 37 | public static void main(String[] args) { 38 | Scanner sc = new Scanner(System.in); 39 | 40 | System.out.print("Enter the number of elements in the array: "); 41 | int n = sc.nextInt(); 42 | 43 | int[] arr = new int[n]; 44 | 45 | System.out.println("Enter the elements in the array: "); 46 | for (int i = 0; i < n; i++) 47 | arr[i] = sc.nextInt(); 48 | 49 | System.out.println("Enter the index of the array element you want to access"); 50 | int index = sc.nextInt(); 51 | try { 52 | System.out.println("The array element at index " + index + " = " + arr[index]); 53 | System.out.println("The array element successfully accessed"); 54 | } catch (ArrayIndexOutOfBoundsException e) { 55 | System.out.println("java.lang.ArrayIndexOutOfBoundsException"); 56 | } 57 | 58 | sc.close(); 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment6/Assignment6.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a Program to take care of Number Format Exception if user enters values other than 3 | integer for calculating average marks of 2 students. The name of the students and marks 4 | in 3 subjects are taken from the user while executing the program. 5 | In the same Program write your own Exception classes to take care of Negative values and 6 | values out of range (i.e. other than in the range of 0-100) 7 | * */ 8 | 9 | package com.w3epic.wiprotraining.assignment6; 10 | 11 | import java.util.Scanner; 12 | 13 | public class Assignment6 { 14 | 15 | public static void main(String[] args) { 16 | Scanner sc = new Scanner(System.in); 17 | 18 | for (int i = 0; i < 2; i++) { 19 | String name = ""; 20 | int subA = 0; 21 | int subB = 0; 22 | int subC = 0; 23 | 24 | try { 25 | name = sc.nextLine(); 26 | 27 | if (sc.hasNextInt()) 28 | subA = sc.nextInt(); 29 | else 30 | throw new NumberFormatException(); 31 | 32 | if (sc.hasNextInt()) 33 | subB = sc.nextInt(); 34 | else 35 | throw new NumberFormatException(); 36 | 37 | if (sc.hasNextInt()) 38 | subC = sc.nextInt(); 39 | else 40 | throw new NumberFormatException(); 41 | 42 | if (subA < 0) throw new NegativeValuesException(); 43 | if (subA > 100) throw new ValuesOutOfRangeException(); 44 | 45 | if (subB < 0) throw new NegativeValuesException(); 46 | if (subB > 100) throw new ValuesOutOfRangeException(); 47 | 48 | if (subC < 0) throw new NegativeValuesException(); 49 | if (subC > 100) throw new ValuesOutOfRangeException(); 50 | 51 | 52 | } catch (ArithmeticException e) { 53 | System.out.println(e.getMessage()); 54 | } catch (NegativeValuesException e) { 55 | System.out.println(e.getMessage()); 56 | } catch (ValuesOutOfRangeException e) { 57 | System.out.println(e.getMessage()); 58 | } 59 | 60 | System.out.println("Name: " + name); 61 | System.out.println("Marks of subject A: " + subA); 62 | System.out.println("Marks of subject B: " + subB); 63 | System.out.println("Marks of subject C: " + subC); 64 | } 65 | 66 | sc.close(); 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/com/w3epic/wiprotraining/assignment3/Assignment3.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes as input the size of the array and the elements in the array. 3 | The program then asks the user to enter a particular index and prints the element at that index. 4 | Index starts from zero. 5 | 6 | This program may generate Array Index Out Of Bounds Exception or NumberFormatException. 7 | Use exception handling mechanisms to handle this exception. 8 | 9 | Sample Input and Output 1: 10 | Enter the number of elements in the array 11 | 2 12 | Enter the elements in the array 13 | 50 14 | 80 15 | Enter the index of the array element you want to access 16 | 1 17 | The array element at index 1 = 80 18 | The array element successfully accessed 19 | 20 | 21 | Sample Input and Output 2: 22 | Enter the number of elements in the array 23 | 2 24 | Enter the elements in the array 25 | 50 26 | 80 27 | Enter the index of the array element you want to access 28 | 9 29 | java.lang.ArrayIndexOutOfBoundsException 30 | 31 | 32 | Sample Input and Output 3: 33 | Enter the number of elements in the array 34 | 2 35 | Enter the elements in the array 36 | 30 37 | j 38 | java.lang.NumberFormatException 39 | * */ 40 | 41 | package com.w3epic.wiprotraining.assignment3; 42 | 43 | import java.util.InputMismatchException; 44 | import java.util.Scanner; 45 | 46 | public class Assignment3 { 47 | 48 | public static void main(String[] args) { 49 | Scanner sc = new Scanner(System.in); 50 | 51 | System.out.println("Enter the number of elements in the arrays"); 52 | int n = sc.nextInt(); 53 | 54 | int[] arr = new int[n]; 55 | 56 | System.out.println("Enter the elements in the array: "); 57 | try { 58 | for (int i = 0; i < n; i++) 59 | arr[i] = sc.nextInt(); 60 | 61 | System.out.println("Enter the index of the array element you want to access"); 62 | 63 | int index = sc.nextInt(); 64 | System.out.println("The array element at index " + index + " = " + arr[index]); 65 | System.out.println("The array element successfully accessed"); 66 | } catch (ArrayIndexOutOfBoundsException e) { 67 | System.out.println("java.lang.ArrayIndexOutOfBoundsException"); 68 | } catch (InputMismatchException e) { 69 | System.out.println("java.util.InputMismatchException"); 70 | } 71 | 72 | sc.close(); 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Wipro TalentNext PBL 2 | 3 | Topics Covered 4 | 5 | Hands-on Assignments for Exception Handling 6 | 7 | 8 | 9 | No. Hands-on Assignment Topics Covered Status 10 | 11 | 1 12 | 13 | Handle exception in number 14 | Problem statement: 15 | Get the input String from user and parse it to integer, if it is not a number it will throw number format exception Catch it and print "Entered input is not a valid format for an integer." or else print the square of that number. (Refer Sample Input and Output). 16 | Sample input and output 1: 17 | Enter an integer: 12 18 | The square value is 144 19 | The work has been done successfully 20 | Sample input and output 2: 21 | Enter an integer: Java 22 | Entered input is not a valid format for an integer. 23 | 24 | Exception Handling 25 | 26 | 2 27 | 28 | Write a program that takes as input the size of the array and the elements in the array. The program then asks the user to enter a particular index and prints the element at that index. 29 | This program may generate Array Index Out Of Bounds Exception. Use exception handling mechanisms to handle this exception. In the catch block, print the class name of the exception thrown. 30 | Sample Input and Output 1: 31 | Enter the number of elements in the array 32 | 3 33 | Enter the elements in the array 34 | 20 35 | 90 36 | 4 37 | Enter the index of the array element you want to access 38 | 2 39 | The array element at index 2 = 4 40 | The array element successfully accessed 41 | 42 | Sample Input and Output 2: 43 | Enter the number of elements in the array 44 | 3 45 | Enter the elements in the array 46 | 20 47 | 90 48 | 4 49 | Enter the index of the array element you want to access 50 | 6 51 | java.lang.ArrayIndexOutOfBoundsException 52 | 53 | Exception Handling: Try-catch 54 | 55 | 3 56 | 57 | Write a program that takes as input the size of the array and the elements in the array. The program then asks the user to enter a particular index and prints the element at that index. Index starts from zero. 58 | 59 | This program may generate Array Index Out Of Bounds Exception or NumberFormatException . Use exception handling mechanisms to handle this exception. 60 | 61 | Sample Input and Output 1: 62 | Enter the number of elements in the array 63 | 2 64 | Enter the elements in the array 65 | 50 66 | 80 67 | Enter the index of the array element you want to access 68 | 1 69 | The array element at index 1 = 80 70 | The array element successfully accessed 71 | 72 | 73 | Sample Input and Output 2: 74 | Enter the number of elements in the array 75 | 2 76 | Enter the elements in the array 77 | 50 78 | 80 79 | Enter the index of the array element you want to access 80 | 9 81 | java.lang.ArrayIndexOutOfBoundsException 82 | 83 | 84 | Sample Input and Output 3: 85 | Enter the number of elements in the array 86 | 2 87 | Enter the elements in the array 88 | 30 89 | j 90 | java.lang.NumberFormatException 91 | 92 | 93 | Exception Handling: Try-catch Use multiple catch block 94 | 95 | 4 96 | 97 | Write a class MathOperation which accepts integers from command line. Create an array using these parameters. Loop through the array and obtain the sum and average of all the elements. 98 | Display the result. 99 | Check for various exceptions that may arise like ArithmeticException, NumberFormatException, and so on. 100 | For example: The class would be invoked as follows: 101 | C:>java MathOperation 1900, 4560, 0, 32500 102 | 103 | Exception handling: throws 104 | 105 | 5 106 | 107 | Write a Program with a division method who receives two integer numbers and performs the division operation. The method should declare that it throws ArithmeticException. This exception should be handled in the main method. 108 | 109 | throws 110 | 111 | 6 112 | 113 | Write a Program to take care of Number Format Exception if user enters values other than integer for calculating average marks of 2 students. The name of the students and marks in 3 subjects are taken from the user while executing the program. 114 | In the same Program write your own Exception classes to take care of Negative values and values out of range (i.e. other than in the range of 0-100) 115 | 116 | Exception Handling: Throw & Used Defined Exception 117 | 118 | 7 119 | 120 | 121 | A student portal provides user to register their profile. During registration the system needs to validate the user should be located in India. If not the system should throw an exception. 122 | Step 1: Create a user defined exception class named “InvalidCountryException”. 123 | Step 2: Overload the respective constructors. 124 | Step 3: Create a main class “UserRegistration”, add the following method, 125 | registerUser– The parameter are String username,String userCountry and add the following logic, 126 | • if userCountry is not equal to “India” throw a InvalidCountryException with the message “User Outside India cannot be registered” 127 | • if userCountry is equal to “India”, print the message “User registration done successfully” 128 | Invoke the method registerUser from the main method with the data specified and see how the program behaves, 129 | Name Country Expected Output 130 | Mickey US InvalidCountryException should be thrown. 131 | The message should be “User Outside India cannot be registered” 132 | Mini India The message should be “User registration done successfully” 133 | Sample Input and Output 134 | 135 | Exception Handling: User Defined Exception & throw 136 | 137 | 8 138 | 139 | 140 | Write a program to accept name and age of a person from the command prompt(passed as arguments when you execute the class) and ensure that the age entered is >=18 and < 60. 141 | Display proper error messages. 142 | The program must exit gracefully after displaying the error message in case the arguments passed are not proper. (Hint : Create a user defined exception class for handling errors.) 143 | 144 | Exception handling: User Defined Exception & throw 145 | 146 | 9 147 | 148 | Write a program that accepts 2 integers a and b as input and finds the quotient of a/b. 149 | This program may generate an Arithmetic Exception. Use exception handling mechanisms to handle this exception. In the catch block, print the message as shown in the sample output. 150 | Also illustrate the use of finally block. Print the message “Inside finally block”. 151 | Sample Input and Output 1: 152 | Enter the 2 numbers 153 | 5 154 | 2 155 | The quotient of 5/2 = 2 156 | Inside finally block 157 | Sample Input and Output 2: 158 | Enter the 2 numbers 159 | 5 160 | 0 161 | DivideByZeroException caught 162 | Inside finally block 163 | 164 | Exception Handling: Finally block --------------------------------------------------------------------------------