import java.io.*;
public class Mine
{
public static void main(String args[])
{
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;
}为啥不是NO such file found,-1,Doing finally,而输出No such file found,Doing finally,-1?

解决方案 »

  1.   

    System.out.println("No such file found");
                return -1;不是先执行完这里再去执行finally?
      

  2.   

    要先执行finally才能return的
    如果直接return了 那finally不就不能执行了吗!
      

  3.   


    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;
      

  4.   

    因为finally语句会在方法返回之前执行,也就是在return之前执行
    LZ查看finally的用法
      

  5.   

    你debug一下就很清楚是如何执行的了