/**
* This code is from the book:
* Winder, R and Roberts, G (1998)
* Developing Java Software
* John Wiley & Sons.
* It is copyright (c) 1997 Russel Winder
* and Graham Roberts.
*/
class l1Except5{//Exception4
//Throw an eception and catch it in the same method.
//This may or may not be useful!
public void f(){
try{
throw new Exception("thrown in f");
}
catch (Exception e){
System.out.println(e.getMessage());
}
}
//Method just throws an exception so must include
//a throws declaration.
public void g() throws Exception{
throw new Exception("thrown in g");
}
public void h() throws Exception{
try{
g();
}
catch (Exception e){
//print a stack trace to see the active
//method chain
e.printStackTrace();
//Reset stack trace so that it starts from here
e. fillInStackTrace();
//Re-throw the exception to let another method
//in the active method chain handle it.
throw e;
}
}
//Catch the exception and prevent further
//propagation.
public void k(){
try{
h();
}
catch (Exception e){
e.printStackTrace();
}
}
//Some extra methods to deepen the stack trace.
public void x(){
k();
}
public void y(){
x();
}
public static void main(String[] args){
l1Except5 e4 = new l1Except5();
e4.f();
e4.y();
}
}
/******** sample compilation & run ******
# javac l1Except5.java
# java l1Except5
thrown in f
java.lang.Exception: thrown in g
at l1Except5.g(l1Except5.java:19)
at l1Except5.h(l1Except5.java:24)
at l1Except5.k(l1Except5.java:43)
at l1Except5.x(l1Except5.java:52)
at l1Except5.y(l1Except5.java:56)
at l1Except5.main(l1Except5.java:62)
java.lang.Exception: thrown in g
at l1Except5.h(l1Except5.java:31)
at l1Except5.k(l1Except5.java:43)
at l1Except5.x(l1Except5.java:52)
at l1Except5.y(l1Except5.java:56)
at l1Except5.main(l1Except5.java:62)
#
******************************************/