• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Exception not being caught  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to catch an exception.
Class1 is calling a method in Class2, an exception is caught and thrown, and this is not being caught in Class1.
public Class1 {
...
try {
Class2 class2 = new Class2();
value = class2.method();
}
catch(Exception e){
System.out.println("This is not happening");
}
}
public class2 {
public synchronized List method() throws Exception {
try {
...
throw new Exception("Bad stuff.");
}
catch (Exception e){
System.out.println("An exception was thrown - this prints");
throw e;
}
finally {
return List;
}
}
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to re-throw the exception from Class2, remove the return statememt from the finally block. The finally block executes before the throw statement and causes the method in Class2 to end before the exception is re-thrown.
 
author
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the finally block is executed AFTER the catch block and therefore after the throw statement in the catch block. Since finally blocks are guarenteed to be executed, the return from the finally block overrides the throw that occured in the catch block.
This is a good example of my rule: Never return from a finally block.
Peter Haggar
------------------
author of: Practical Java
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!