Skip to content

Commit f58f0b5

Browse files
authored
Create ExceptionMulti.java
1 parent bad899a commit f58f0b5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

ExceptionMulti.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Demonstrating multiple catching of Exceptions
2+
3+
import java.util.*;
4+
class ExceptionMulti
5+
{
6+
public static void main(String args[])
7+
{
8+
int a[] = {5,10};
9+
int b = 5;
10+
11+
try
12+
{
13+
int x = a[2] / b - a[1]; // a[2] is non-existant
14+
System.out.println("x = " + x);
15+
}
16+
catch(ArithmeticException e)
17+
{
18+
System.out.println("Division by Zero"); // never happen in this code
19+
}
20+
catch(ArrayIndexOutOfBoundsException e)
21+
{
22+
System.out.println("Array Index Error"); // We will get this
23+
}
24+
catch(ArrayStoreException e)
25+
{
26+
System.out.println("Wrong Datatype"); // Will be ignored
27+
}
28+
finally
29+
{
30+
int y = a[1] / a[0];
31+
System.out.println("y = " +y); // a = 2
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)