File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the concept behind? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the concept behind?" Watch "what is the concept behind?" New topic
Author

what is the concept behind?

fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
hi, there
could someone explain to me what the different function between"return -1" and "return 0", and why -1 got printed out and 0 not?
import java.io.*;
public class Mine {
public static void main(String argv[]){
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod() {
try {
FileInputStream dis=new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
return -1;
}catch(IOException ioe) {
} finally{
System.out.println("Doing finally");
}
return 0;
}
}
thanks in advance....
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
As far as i know..
"could someone explain to me what the different function between"return -1" and "return 0", and why -1 got printed out and 0 not?"
It is for debugging purpose. Returning 0 means the function
ran normally. Returning -1 means there were a error.
Hope this help.
Anyone have more input on it?
Chris Linde
Greenhorn

Joined: Oct 15, 2001
Posts: 10
Hello Fengqiao,
I think I understand the source of your confusion, but please elaborate if I misunderstand you. If you are wondering why it is that the method returns -1 instead of returning 0 , then one has to understand that when a statement in a try-block throws an exception, the execution flows down to the matching catch statement. If there is not a catch-block that can deal with the type of exception thrown in the try-block, the execution procedes directly to the finally statement, and then the execution flows up to the method that called the method containing the try-block, the rest of the code inside the method that contained the try-block does NOT execute. Therefore the return 0 statement at the bottom of the method never executes. If you want this method to return 0, place the return 0 statement inside the finally block, where it will override the return -1 statement in the catch-block.
finally{
System.out.println("Doing finally");
return 0;
}
Hope I helped!
fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
hi,chris
thanks for your reply. i see what you mean but look at the following code:
output:finally done
returning from division
returning from main
class DivisionByZeroException extends Exception{
DivisionByZeroException(String msg) {super(msg);}
}
public class DivisionByZero5{
public void division(){
int num1 =10;
int num2 =0;
try{
if (num2==0) throw new DivisionByZeroException("/by o");
System.out.println(num1+"/"+num2 +"=" +(num1/num2));
}catch(DivisionByZeroException e){
System.out.println("deal with" +e);
}finally { System.out.println("finally done");
}
System.out.println("returning from division");
}

public static void main(String args[]){
new DivisionByZero5().division();
System.out.println("retruning from main");

}
}
fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
hi, chris
i think i got it now. the "return statement" stop the current excution and return the control back to the caller. but i am a little confused if like what i said why the finally got executed?( i know it is defined in java doc. that "finally block" always got executed)
fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
hi,
i found new stuff.
the output of the following code is:
no such file found
doing fianlly
-2
i tried to not comment line 1 the output is -1 and i also tried to comment line 3, but compile erro. From the output, we can understand that finally block executed first and then "return statement". I assum that the line 3 must be executed anytime, but why not returen the value in line 3?

import java.io.*;
public class Mine { //start of class mine
public static void main(String argv[]){
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod() { //start of amethod()method
try {
FileInputStream dis=new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
//return -1; //1
}catch(IOException ioe) { //return 0;
} finally{
System.out.println("Doing finally");
//return -1; //2
}
return -2; //3
}//end of amethod()method.
}//end of class mine
David Cuesta
Greenhorn

Joined: Nov 07, 2001
Posts: 25
Hello fenqiao,
I only know a think about this, The block finally always is executed, even the exeception it`s not throwed.
this occurs even when a return stament is before the finally block, after this the return stament is executed.
I hope that this help you.
byez
Originally posted by fengqiao cao:
hi,
i found new stuff.
the output of the following code is:
no such file found
doing fianlly
-2
i tried to not comment line 1 the output is -1 and i also tried to comment line 3, but compile erro. From the output, we can understand that finally block executed first and then "return statement". I assum that the line 3 must be executed anytime, but why not returen the value in line 3?

import java.io.*;
public class Mine { //start of class mine
public static void main(String argv[]){
Mine m=new Mine();
System.out.println(m.amethod());
}
public int amethod() { //start of amethod()method
try {
FileInputStream dis=new FileInputStream("Hello.txt");
}catch (FileNotFoundException fne) {
System.out.println("No such file found");
//return -1; //1
}catch(IOException ioe) { //return 0;
} finally{
System.out.println("Doing finally");
//return -1; //2
}
return -2; //3
}//end of amethod()method.
}//end of class mine

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141

i am a little confused if like what i said why the finally got
executed?( i know it is defined in java doc. that "finally block" always got executed)

The 'finally' block is always executed. It doesn't matter if the code completes normally or throws an exception.
Hope that helps.


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
HanMing Low
Ranch Hand

Joined: Oct 18, 2001
Posts: 196
Hi,
Not too sure this explanation will help.
In your case, I think you're confused which return statement should be called and why.
Put it this way.
try
{
//block 1 : do something
}
catch (Exception ex)
{
//block 2 : do something else if problem encounter in block 1
return -1;
}
finally
{
//block 3 : do this irregardless of situation
}
return 0;
In this case, what you're trying to achieve is that if something goes wrong in block 1, catch it in block 2 and finished in block 3.
You wouldn't want return 0 to run as return 0 mean everything is fine.
So, you insert return -1 in block 2.
If everything goes fine, then the method will run return 0 because block 2 will not be run showing everything is fine.
If you don't insert return -1 in block 2, return 0; WILL run if the error is catch in block 2. (i.e. unless you throw some error which is not catch by your catch statement)
Does this make sense?
Hope it helps.

Han Ming
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the concept behind?