0% found this document useful (0 votes)
17 views

Assignment No. 4 GRN: 12120127 Name of The Student: Waghule Shubham Kalyan Roll No.: 94 Class: CS Division: D Batch: B3 Problem Statement

The document describes an assignment to write a program that handles different exceptions, including dividing by zero, accessing an array out of bounds, and calling a method on a null value. It provides sample inputs and expected outputs for each exception. The full code is included for a menu-driven program that allows the user to select an exception and then generates the appropriate error message in a try-catch block.

Uploaded by

WAGHULE SHUBHAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment No. 4 GRN: 12120127 Name of The Student: Waghule Shubham Kalyan Roll No.: 94 Class: CS Division: D Batch: B3 Problem Statement

The document describes an assignment to write a program that handles different exceptions, including dividing by zero, accessing an array out of bounds, and calling a method on a null value. It provides sample inputs and expected outputs for each exception. The full code is included for a menu-driven program that allows the user to select an exception and then generates the appropriate error message in a try-catch block.

Uploaded by

WAGHULE SHUBHAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment No.

GRN: 12120127
Name of the Student: Waghule Shubham Kalyan
Roll No.: 94
Class: CS
Division: D
Batch: B3

Problem Statement
Write a program for following exception, develop a suitable scenario in which the following
exceptions occur:
a. divide by zero
b. Array index out of bounds exception
c. Null pointer Exception

Develop a menu driven program for handle the above listed exception.

Sample Input and Output

Sample Input/Parameter Expected Output


Try to divide a number by zero You shouldn’t
divide a number by
zero.
 Try to access the array index which does OOPs!!!Array Index
not exist. 7 out of bounds for
length 6.
Try to find the length of String in method Null Pointer
(pass parameter string as null) Exception arises!!

import java.util.Scanner;

public class tester2 {


        public static void main(String[] args) {
                Scanner s = new Scanner(System.in);
                System.out.println("Which exception do you want to raise?");
                System.out.println(
                                "\n1]Divide by zero exception\n2]Array index
out of bound exception\n3]Null pointer exception");
                int choice = s.nextInt();
                try {
                        if (choice == 1) {
                                System.out.println("Enter value 1: ");
                                int n1 = s.nextInt();
                                System.out.println("Enter value 2: ");
                                int n2 = s.nextInt();
                                int val = n1 / n2;
                                System.out.println("Answer: " + val);
                        } else if (choice == 2) {
                                int arr[] = new int[] { 1, 2, 3 };
                                System.out.println("Enter the index of
element you want to access: ");
                                int index = s.nextInt();
                                System.out.println("Value: " + arr[index]);
                        } else if (choice == 3) {
                                String str = null;
                                System.out.println(str.length());
                        }
                } catch (ArithmeticException e) {
                        System.out.println("You shouldn't divide a number by
zero" + e);
                } catch (NullPointerException e) {
                        System.out.println("Null pointer exception arises" +
e);
                } catch (ArrayIndexOutOfBoundsException e) {
                        System.out.println("OOPs!!! Array Index out of bound
exception" + e);
                }
                s.close();
        }
}

Expected Output:

You might also like