File tree Expand file tree Collapse file tree
bin/com/cdac/customexception Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /com /
Original file line number Diff line number Diff line change 1+ package com .cdac ;
2+
3+ /*
4+ * Exception Hierarchy Chaining
5+ * multiple catch block in Hierarchy order
6+ */
7+ public class ExceptionChaining {
8+
9+ public static void main (String [] args ) {
10+ try {
11+ //test case 1 - Arithmetic Exception
12+ int i = 10 / 0 ;
13+
14+ //comment the above code and run this code
15+ //test case 2 - Runtime exception with return statement
16+ String s =null ;
17+ s .length ();
18+ }catch (ArithmeticException ae ){
19+ System .out .println ("Arithmetic Exception" );
20+ ae .printStackTrace ();
21+ }catch (RuntimeException re ){
22+ System .out .println ("Runtime Exception" );
23+ re .printStackTrace ();
24+ //this return statement transfer the control to calling method
25+ //because this return, code statement after try block will not execute
26+ return ;
27+ }catch (Exception e ){
28+ System .out .println ("Exception" );
29+ e .printStackTrace ();
30+ }
31+ finally {
32+ //this block will execute in both cases
33+ //with exception or without exception occurs
34+ System .out .println ("Finally block" );
35+ }
36+ System .out .println ("Outside of try block" );
37+ }
38+ }
Original file line number Diff line number Diff line change 1+ package com .cdac ;
2+
3+ /*
4+ * This method will throw the exception
5+ * But the method returns the value also.
6+ */
7+
8+ public class ExceptionWithReturn {
9+
10+ public static void main (String [] args ) {
11+ boolean result = nullCheck (null );
12+ System .out .println ("Result is :: " +result );
13+ }
14+
15+ public static boolean nullCheck (String s ){
16+ try {
17+ if (s == null )
18+ throw new ArithmeticException ();
19+ System .out .println ("After exception" );
20+ }catch (ArithmeticException e ){
21+ e .printStackTrace ();
22+ return false ;
23+ }
24+ finally {
25+ System .out .println ("Finally Block" );
26+ }
27+ return true ;
28+ }
29+
30+ }
Original file line number Diff line number Diff line change 1+ package com .cdac ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .File ;
5+ import java .io .FileNotFoundException ;
6+ import java .io .FileReader ;
7+ import java .io .IOException ;
8+
9+ public class FileHandlingException {
10+
11+ public static void main (String [] args ) {
12+ try {
13+ File file = new File ("D:\\ abc.txt" );
14+ BufferedReader bufferedReader = new BufferedReader (new FileReader (file ));
15+ bufferedReader .readLine ();
16+ bufferedReader .close ();
17+ System .out .println ("End of the program!" );
18+ } catch (FileNotFoundException e ) {
19+ //when the file is not available in the path
20+ //this exception will be thrown
21+ e .printStackTrace ();
22+ } catch (IOException e ) {
23+ //when there is an issue in input/output of the file
24+ //this exception will be thrown
25+ e .printStackTrace ();
26+ }
27+ }
28+ }
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ public static void main(String[] args) {
1111 System .out .println ("Student name is :" +s .getName ());
1212 } catch (StudentNotFoundException e ) {
1313 System .out .println (e .getMessage ());
14+ e .printStackTrace ();
1415 }
1516 }
1617}
You can’t perform that action at this time.
0 commit comments