Chained Exceptions in Java
Run the Program
If you haven't already done so, I encourage you to copy the code from Listing 7 into your text editor, compile it, and execute it. Experiment with it, making changes, and observing the results of your changes.Remember, however, that you must be running Java version 1.4.0 or later to compile and execute this program.
Complete Program Listing
/*File Excep20.java Copyright 2002, R. G. Baldwin Illustrates chained exceptions as supported by JDK 1.4.0 Tested using JDK 1.4.0 under Win2000 The output produced by the program is similar to the following: In meth02 catch block Msg is: Msg from metho03 Cause is: java.lang.ArithmeticException: / by zero In meth01 catch block Msg is: Msg from meth02 Cause is: java.lang.IndexOutOfBoundsException: Msg from metho03 In main catch block Msg is: Msg from meth01 Cause is: NewEx02: Msg from meth02 Print StackTrace NewEx01: Msg from meth01 at Class01.meth01(Excep20.java:124) at Excep20.main(Excep20.java:61) Caused by: NewEx02: Msg from meth02 at Class01.meth02(Excep20.java:141) at Class01.meth01(Excep20.java:115) ... 1 more Caused by: java.lang.IndexOutOfBoundsException: Msg from metho03 at Class01.meth03(Excep20.java:151) at Class01.meth02(Excep20.java:132) ... 2 more Caused by: java.lang.ArithmeticException: / by zero at Class01.meth03(Excep20.java:149) ... 3 more **************************************/ import java.io.*; class Excep20{ public static void main( String[] args){ try{ new Class01().meth01(); }catch(NewEx01 e){ System.out.println( "In main catch block"); System.out.println( "Msg is:\n" + e.getMessage()); System.out.println( "Cause is:\n" + e.getCause()); System.out.println();//blank line System.out.println( "Print StackTrace"); e.printStackTrace(); }//end catch }//end main }//end Excep20 //===================================// //This is a new exception class class NewEx01 extends Exception{ public NewEx01() { } public NewEx01(String message){ super(message); } public NewEx01(Throwable throwable){ super(throwable); } public NewEx01(String message, Throwable throwable){ super(message, throwable); } }//end NewEx01 //===================================// //This is a new exception class class NewEx02 extends Exception{ public NewEx02() { } public NewEx02(String message){ super(message); } public NewEx02(Throwable throwable){ super(throwable); } public NewEx02(String message, Throwable throwable){ super(message, throwable); } }//end NewEx02 //===================================// class Class01{ void meth01() throws NewEx01{ try{ meth02(); }catch(NewEx02 e){ System.out.println( "In meth01 catch block"); System.out.println( "Msg is:\n" + e.getMessage()); System.out.println( "Cause is:\n" + e.getCause()); System.out.println();//blank line throw new NewEx01( "Msg from meth01",e); }//end catch }//end meth01 //---------------------------------// void meth02() throws NewEx02{ try{ meth03(); }catch(RuntimeException e){ System.out.println( "In meth02 catch block"); System.out.println( "Msg is:\n" + e.getMessage()); System.out.println( "Cause is:\n" + e.getCause()); System.out.println(); throw new NewEx02( "Msg from meth02",e); }//end catch }//end meth02 //---------------------------------// void meth03(){ try{ int x = 3/0; }catch(ArithmeticException e){ IndexOutOfBoundsException ex = new IndexOutOfBoundsException( "Msg from metho03"); ex.initCause(e); throw ex; }//end catch }//end meth03 //---------------------------------// }//end Class01 Listing 7 |
Copyright 2002, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.
About the author
Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java, C#, and XML. In addition to the many platform and/or language independent benefits of Java and C# applications, he believes that a combination of Java, C#, and XML will become the primary driving force in the delivery of structured information on the Web.Richard has participated in numerous consulting projects, and he frequently provides onsite training at the high-tech companies located in and around Austin, Texas. He is the author of Baldwin's Programming Tutorials, which has gained a worldwide following among experienced and aspiring programmers. He has also published articles in JavaPro magazine.
Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.
-end-