33--||author : codechaser||--
44~~~~~~~~~~~~~~~~~~~~~~~~~~~
55*/
6- package java_labs .lab_4 .problem_3 ;
6+ package java_labs .lab_5 .problem_3 ;
77
88import java .util .*;
99import java .io .*;
10- import java_labs .lab_4 .problem_3 .example .*;
1110
1211public class Problem3 {
12+ private static void method1 (int i ) {
13+ if (i == 1 ) {
14+ int x = i / (i - i ); // throws ArithmeticException;
15+ }
16+ else {
17+ Vector <Integer > v = new Vector <Integer >(0 );
18+ int x = v .get (27 ); // throws ArrayIndexOutOfBoundsException;
19+ }
20+ return ;
21+ }
22+
23+ private static void method2 (int i ) throws ArithmeticException , ArrayIndexOutOfBoundsException , FileNotFoundException {
24+ if (i == 1 )
25+ throw new ArithmeticException ("Checked ArithmeticException Thrown from method 2\n " );
26+ if (i == 2 )
27+ throw new ArrayIndexOutOfBoundsException ("Checked ArrayIndexOutOfBoundsException Thrown from method 2\n " );
28+ else
29+ throw new FileNotFoundException ("Checked FileNotFoundException Thrown from method 2\n " );
30+ }
31+
1332 public static void start () {
1433 while (true ) {
1534 Scanner input = new Scanner (System .in );
1635 System .out .println ("\n \n ---------------------------" );
1736 System .out .println (" P R O B L E M 3" );
1837 System .out .println ("---------------------------\n " );
19- System .out .println ("[01] : PublicClassA Content" );
20- System .out .println ("[02] : PublicClassB Content" );
21- System .out .println ("[03] : PublicClassC Content\n " );
38+ System .out .println ("[01] : Throw Unchecked Exceptions" );
39+ System .out .println ("[02] : Throw Checked Exceptions\n " );
2240 System .out .println ("[-1] : Exit\n " );
2341 System .out .println ("---------------------------\n " );
2442 System .out .println ("Enter your choice :\n " );
@@ -32,27 +50,49 @@ public static void start() {
3250 }
3351 switch (choice ) {
3452 case 1 :
35- PublicClassA A = new PublicClassA ();
36- A .method1 ();
37- A .method2 ();
53+ try {
54+ method1 (1 );
55+ } catch (ArithmeticException e ) {
56+ System .out .println ("Caught unchecked ArithmeticException from method1();" );
57+ System .out .println (e + "\n " );
58+ } finally {
59+ System .out .println ("'finally block 1' reached!\n " );
60+ }
61+ try {
62+ method1 (2 );
63+ } catch (ArrayIndexOutOfBoundsException e ) {
64+ System .out .println ("Caught unchecked ArrayIndexOutOfBoundsException from method1();" );
65+ System .out .println (e + "\n " );
66+ } finally {
67+ System .out .println ("'finally block 2' reached!\n " );
68+ }
3869 System .out .println ("\n ---------------------------\n " );
3970 break ;
4071 case 2 :
41- PublicClassB B = new PublicClassB ();
42- B .method1 ();
43- B .method2 ();
44- System .out .println ("\n ---------------------------\n " );
45- break ;
46- case 3 :
47- PublicClassC C = new PublicClassC ();
48- C .method1 ();
49- C .method2 ();
50- PublicClassC .Class1 C1 = C .new Class1 ();
51- C1 .method ();
52- PublicClassC .Class2 C2 = C .new Class2 ();
53- C2 .method ();
54- PublicClassC .Class3 C3 = C .new Class3 ();
55- C3 .method ();
72+ try {
73+ method2 (1 );
74+ } catch (Exception e ) {
75+ System .out .println ("Caught checked ArithmeticException from method2();" );
76+ System .out .println (e + "\n " );
77+ } finally {
78+ System .out .println ("'finally block 1' reached!\n " );
79+ }
80+ try {
81+ method2 (2 );
82+ } catch (Exception e ) {
83+ System .out .println ("Caught checked ArrayIndexOutOfBoundsException from method2();" );
84+ System .out .println (e + "\n " );
85+ } finally {
86+ System .out .println ("'finally block 2' reached!\n " );
87+ }
88+ try {
89+ method2 (3 );
90+ } catch (Exception e ) {
91+ System .out .println ("Caught checked FileNotFoundException from method2();" );
92+ System .out .println (e + "\n " );
93+ } finally {
94+ System .out .println ("'finally block 3' reached!\n " );
95+ }
5696 System .out .println ("\n ---------------------------\n " );
5797 break ;
5898 default :
0 commit comments