Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • 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???
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ]
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sagar. The link given by you contains apt explanation for the given question.
 
harilal ithikkat
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • 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: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • 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: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic