*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks Sagar. The link given by you contains apt explanation for the given question.


My Blog SCJP 5 SCWCD 5
harilal ithikkat
Ranch Hand

Joined: Oct 06, 2008
Posts: 221
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???


SCJP 1.5
"A candle looses nothing by lighting another candle"
itechmentors.com
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Feb 10, 2008
Posts: 179

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?


-kolli
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...
 
 
subject: Exceptions
 
Similar Threads
ExamLab Exception
Exceptions question
Exception Handling
Can somebody please explain me the output
Exceptions doubt?