ExceptionExample.
java 29-01-2022 04:37
1 /*
2 Example of Exception and Exception
3 Handling.
4 */
5 class ExceptionExample
6 {
7 public static void main(String[] args)
8 {
9 int a = Integer.parseInt(args[0]);
10 int b = Integer.parseInt(args[1]);
11 int c = a/b;
12 System.out.println("Value of c = "+c);
13 }
14 }
15
Page 1 of 1
Different Exception got by the above program.
ExceptionExampleWithSol.java 29-01-2022 04:35
1 /*
2 Example of Exception and Exception
3 Handling.
4 use of try and catch.
5 */
6 class ExceptionExampleWithSol
7 {
8 public static void main(String[] args)
9 {
10 try{
11 int a = Integer.parseInt(args[0]);
12 int b = Integer.parseInt(args[1]);
13 int c = a/b;
14 System.out.println("Value of c = "+c);
15 }
16 catch(ArrayIndexOutOfBoundsException aiob)
17 {
18 System.out.println("Pls pass 2 int values");
19 }
20 catch(NumberFormatException nfe)
21 {
22 System.out.println("Pls pass integer values");
23 }
24 catch(ArithmeticException ae)
25 {
26 System.out.println("Pls dont pas 2 val ZERO");
27 }
28 }
29 }
30
Page 1 of 1
Output after Exception handling done by try and catch in the above program.