aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try and catch 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 "try and catch " Watch "try and catch " New topic
Author

try and catch

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
Why is the return 1 is not printed at all ???

public class Test1{
public static void main(String args[]){
System.out.println(method());
}
public static int method(){
try{
return 1;//////////////////////////////////////////???/
}
catch(Exception e){
return 2;
}
finally{
return 3;
}
}
}
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
hi

when i compile the above code
I am getting error as below

---------- Javac ----------
warning: finally clause cannot complete normally
}
^
--------------------------------------------------------
harish shankarnarayan
Ranch Hand

Joined: Sep 12, 2005
Posts: 158
hey its compiles fine

in the above example first its returning value 1.
after that as finally executes always its returning 3
and u r getting the output 3
remove finally u will get output 1.

i hope so u got it..


Harish<br />SCJP 1.4 (85%)
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
It works for me and output is 3.
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
hi all
I am using jdk 1.4
Raghusham Sankargal
Ranch Hand

Joined: Oct 10, 2005
Posts: 59
Hello,
Its a warning not an error. Did you try java <class-name>. Tell me if you get the error. I don't think you should get.


Raghusham Sankargal
Vijayagopalan Raveendran
Ranch Hand

Joined: Apr 18, 2005
Posts: 41
finally block is executed irrespective of whether exception is thrown or not.


Vijay<br />SCJP 1.5 / SCWCD 1.4 / SCDJWS 1.4 / SCBCD 5/ SCEA 5 (P1)/ SOA 667
Vijayagopalan Raveendran
Ranch Hand

Joined: Apr 18, 2005
Posts: 41
finally block is executed irrespective of whether exception is thrown or not.
Gyan Shankar
Ranch Hand

Joined: Dec 12, 2005
Posts: 65
The finally return always overrides the try return


SCJP(1.4), SCWCD(1.4), SCBCD(1.3), SCDJWS
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: try and catch