Skip to content

Commit db41af1

Browse files
Array index and Number format Exception
1 parent d009e41 commit db41af1

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

exceptionHandling/MyException.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
class MyException
3+
{
4+
public static void main(String args[])
5+
{
6+
try
7+
{
8+
Scanner sc = new Scanner(System.in);
9+
System.out.println("Enter the size of the array");
10+
int size = Integer.parseInt(sc.next());
11+
int[] arr = new int[size];
12+
for(int i=0;i<arr.length;i++)
13+
{
14+
arr[i]=Integer.parseInt(sc.next());
15+
}
16+
System.out.println("Enter a index no. Index starts from zero");
17+
int index = Integer.parseInt(sc.next());
18+
System.out.println(arr[index]);
19+
}
20+
catch(ArrayIndexOutOfBoundsException | NumberFormatException e)
21+
{
22+
System.out.println(e.getClass().getName());
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)