-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.java
More file actions
52 lines (52 loc) · 1.47 KB
/
Array.java
File metadata and controls
52 lines (52 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Program: Handle Array Input with Exception Handling
// Topic: Arrays and Exception Handling
// Description: Takes an array of student marks as input and demonstrates use of try-catch-finally blocks.
// Catches ArrayIndexOutOfBoundsException, InputMismatchException, and generic Exception to show different error cases.
package Array;
import java.util.*;
/**
*
* @author Samim
*/
public class Array
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter N :");
n=sc.nextInt();
int ar[]=new int[n];
try{
for(int i=0;i<=n;i++)
{
System.out.println("Enter Marks of student "+(i+1)+":");
ar[i]=sc.nextInt();
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Out Of Bound");
for(int i=0;i<n;i++)
{
System.out.println("Marks["+(i)+"]="+ar[i]);
}
}
catch(InputMismatchException e)
{
System.out.println("Number Format Exception");
}
catch(Exception e)
{
System.out.println("Parent Exception");
for(int i=0;i<n;i++)
{
System.out.println("Marks["+(i)+"]="+ar[i]);
}
}
finally
{
System.out.println("Finally");
}
}
}