-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcep.java
More file actions
64 lines (63 loc) · 1.59 KB
/
excep.java
File metadata and controls
64 lines (63 loc) · 1.59 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
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.*;
class excep
{
static int size,i;
static int a[];
static int sum=0;
public static void main(String args[])
{
System.out.println("\t\t\tException Handling");
System.out.println("\t\t\t------------------");
Scanner s=new Scanner(System.in);
try
{
System.out.println("Enter the size : ");
size=Integer.parseInt(s.next());
}
catch(NumberFormatException ne)
{
System.out.println("(Number Format Exception)Trying to convert string into integer");
System.out.println("\nEnter the size in integer: ");
size=s.nextInt();
}
try
{
a=new int[size];
}
catch(NegativeArraySizeException n)
{
System.out.println("(Negative Array Size Exception)Enter the size of array in Positive : ");
size=s.nextInt();
a=new int[size];
}
try
{ System.out.println("\nEnter the elements one by one:");
for(i=0;i<=size;i++)
a[i]=s.nextInt();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("(Array Index out of Bounds Exception)Index value out of bound");
System.out.println("\nOnce Again enter the elements one by one:");
for(i=0;i<size;i++)
{
a[i]=s.nextInt();
sum=sum+a[i];
}
System.out.println("\tSum of array : "+sum);
}
try
{
System.out.println("\nEnter a denominator to divide the sum:");
int d=s.nextInt();
int r=sum/d;
}
catch(ArithmeticException ex)
{
System.out.println("\n(Arithmetic Exception)Enter a non-zero denominator : " );
int d=s.nextInt();
int r=sum/d;
System.out.println("\n\t\tAverage of an array : "+sum+"/"+d+"="+r);
}
}
}