当前位置: 技术问答>java相关
简单的异常问题,帮忙,多谢
来源: 互联网 发布时间:2015-02-17
本文导语: 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) { ...
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;
}
}
的输出为什么是No such file found, Doing finally, -1 ?而不是
No such file found, Doing finally, 0???多谢!
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;
}
}
的输出为什么是No such file found, Doing finally, -1 ?而不是
No such file found, Doing finally, 0???多谢!
|
实际的执行顺序是先执行catch中的语句,如果有return,则查找是否有finally,有则执行之,如果finally中有return,则执行它的return,否则返回catch执行return。如果catch中没有return,则执行完catch后执行finally,再执行try/catch/finally后的语句。
|
这又什么奇怪的???
本来就是返回-1呀
finally中的当然是在return之前执行
本来就是返回-1呀
finally中的当然是在return之前执行