• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exceptions

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Inquisition

class test {
test() {
try {
throw new RuntimeException();
} finally {
System.out.println("Damn !");
}
}
public static void main( String args[] ) {
try {
new test();
} catch ( Throwable t ) {
System.out.println("Caught");
}
}
}

The answer is Damn!
Caught.


class test {
test() {
try {
throw new RuntimeException();
} finally {
return;
}
}
public static void main( String args[] ) {
try {
new test();
} catch ( Throwable t ) {
System.out.println("Caught");
}
}
}

But when there is a return in finally the Exception is not caught.why is it so? Does finally negate the exception???
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes abhi. whatever finally does, is the final output of the method. If an exception is thrown in finally, then any return value or exception pending is discarded. If finally uses a return statement, an return value or exception pending is discarded. So basically finally can override any return value or exception of the associated try-catch block...
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abhi vijay:
Does finally negate the exception???



Nope, but the abrupt return from finally block override the whatever the previous exist statements did , i.e whether you returned some value or thrown some exception, the call to return , exits from function without any information..

Look out this similar discussion !

If I'm wrong or missing something , then correct me Ranchers !!

By the way, Here is some modification to your previous code, to explain more briefly



output : Caught



And please quote your CODE ~~
[ December 03, 2008: Message edited by: Sagar Rohankar ]
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sagar. The link given by you contains apt explanation for the given question.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but let me ask you one thing.....
theoritically returning a value by constructor is not a valid thing isn't it
do you mean that test as method or constructor???
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by harilal ithikkat:
but let me ask you one thing.....
theoritically returning a value by constructor is not a valid thing isn't it
do you mean that test as method or constructor???



Constructors can have a simple return statement which has no value...
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankit Garg:


Constructors can have a simple return statement which has no value...



Is it because in this case return is considered as just a branching statement? As we do know that to return a value we should declare a return type for the method which is not done in the case of constructor?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes. Just as you can use a simple return statement in a void method, you can use return in constructor where it will act just as a control statement...
reply
    Bookmark Topic Watch Topic
  • New Topic