[go: up one dir, main page]

0% found this document useful (0 votes)
44 views4 pages

Satvik Rana

This experiment involves writing a Java program to handle ArrayIndexOutOfBounds exceptions. The program takes the size of an array and elements as input, then prompts the user for an index to print. If the index is invalid, an ArrayIndexOutOfBoundsException is thrown and caught. The catch block prints the exception class name. Key aspects of exception handling learned are using try/catch blocks to test for errors, and executing catch blocks if errors occur in try blocks. The experiment teaches how exceptions disrupt normal program flow and should be handled.

Uploaded by

Manas Khare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Satvik Rana

This experiment involves writing a Java program to handle ArrayIndexOutOfBounds exceptions. The program takes the size of an array and elements as input, then prompts the user for an index to print. If the index is invalid, an ArrayIndexOutOfBoundsException is thrown and caught. The catch block prints the exception class name. Key aspects of exception handling learned are using try/catch blocks to test for errors, and executing catch blocks if errors occur in try blocks. The experiment teaches how exceptions disrupt normal program flow and should be handled.

Uploaded by

Manas Khare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment 3.

Name: Satvik Rana UID: 19BCS1662


Branch: CSE Section/Group: 8 b
Date of Performance
Subject Name: Java Programming Subject Code- CSP-205

1. Aim/Overview of the practical:


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. 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.
2. Task to be done/ Which logistics used:
We have to do Exception Handling in this experiment.

3. Algorithm:
STEP 1 – START

STEP 2 -We take the size of the array from the user.
STEP 3-Inside the try block we take the elements of the array from the user and insert the elements into the array one
by one.
STEP 4-Inside the try block we take the index of the array from the user that user want to print.
STEP 5-If the index entered by the user is in between 0 and length of the array -1 then print the element of the array
present at that index
STEP 6-If the index entered by the user greater than or equal to the size of the array it will go to that catch block and
print ArrayIndexOutOfBound Exception.
STEP 7- END
4. Code:
import java.util.Scanner; public

class Array {

public static void main(String[] args) { Scanner


sc=new Scanner(System.in);
System.out.println("Enter the number of elements in the array"); int
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the elements in the array"); try {
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("Enter the index of the array element you want to access:"); int
j=sc.nextInt();

System.out.println("The array element at index "+ j+":"+a[j]);


System.out.println("The Array Element successfully Accessed");
}catch(Exception e)
{
System.out.println(e);
}

}
5. Observations/Discussions/ Complexity Analysis:
The ArrayIndexOutOfBoundsException occurs whenever we are trying to access any item of an array at an
index which is not present in the array. In other words, the index may be negative or exceed the size of an
array. An exception (or exceptional event) is a problem that arises during the execution of a program. When
an Exception occurs the normal flow of the program is disrupted and the program/Application terminates
abnormally, which is not recommended, therefore, these exceptions are to be handled.

6. Result/Output/Writing Summary:
7. Learning outcomes (What I have learnt):

1. A method catches an exception using a combination of the try and catch keywords. A try/catch
block is placed around the code that might generate an exception. Code within a try/catch block is
referred to as protected code

2. The try statement allows you to define a block of code to be tested for errors while it is being
executed.

3. The catch statement allows you to define a block of code to be executed, if an error occurs in the
try block.

4. An exception is an unwanted or unexpected event,which occur during the execution of the


program i.e. at runtime that dispute the normal flow of the program.

8. Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like