Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

can anyone answer this problem

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules: Formulate your answer before you run the code.

The code:

public class TestClass {

/**
*
*/
public TestClass() {
super();
}

public boolean foo() throws ArithmeticException {
try {
int Nan = 1 / 0;
} catch (ArithmeticException e) {
throw e;
} finally {
return true;
}

}

public static void main(String[] args) {
TestClass test = new TestClass();
boolean result = false;
try {
result = test.foo();
} catch (ArithmeticException e) {
e.printStackTrace();
}

System.out.println(result);
}

}
The question:

What gets printed to the console?


a) stack trace, then false
b) stack trace, then true
c) false
d) true

Why?
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, it's more sensible to have a subject that describes the posting.

This problem is weird. I tried it out on Eclipse with 1.5 and 1.4. The exception is being thrown out of the first method but not out of the second one. It's puzzling.

Doesn't this amount to suppressing the exception by using a finally clause?

Somebody explicate this.
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At my end it is showing only true as output.

Originally posted by Stuart Ash:

Doesn't this amount to suppressing the exception by using a finally clause?
Somebody explicate this.



I am agree with you.
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You did not mention the compiler warning.

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Stuart Ash]: The exception is being thrown out of the first method but not out of the second one. It's puzzling.

I can't tell what you mean by "first method" and "second one". Was the output "true" both times? That's what should happen.

Doesn't this amount to suppressing the exception by using a finally clause?

Yes, this is what normally happens if you either return or throw an exception from within a finally clause - the JVM completely forgets what it was doing (i.e. any original exception that was being thrown is forgotten with no trace) and instead the JVM obeys the newer instruction, returning or throwing a new exception accordingly. Which is almost always a bad thing; hence the compiler warning (which I would prefer were a compiler error, but maybe there are a few rare cases where it's not - I doubt it though).
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see my reply at
https://coderanch.com/t/327609/java/java/anyone-answer-why-it

Why two posting?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have highlighted, once again, the "lost exception" (steal Bruce Eckel's terminology).

http://www.google.com/search?hl=en&lr=&q=java+%22the+lost+exception
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would expect the compile warning. its obligated to respect the finally clause.
reply
    Bookmark Topic Watch Topic
  • New Topic