try catch finally与return的执行顺序
2008-01-05 08:46:44 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁惧墽鎳撻—鍐偓锝庝簻椤掋垺銇勯幇顖毿撻柟渚垮妼椤粓宕卞Δ鈧獮濠勭磽閸屾艾鈧懓顫濋妸鈺佺疅缂佸顑欓崥瀣煕椤愵偅绶氱紓鍐╂礋濮婂宕掑▎鎴М濠电姭鍋撻梺顒€绉甸幆鐐哄箹濞n剙濡肩紒鎰殜閺屸€愁吋鎼粹€茬敖婵炴垶鎸哥粔鐢稿Φ閸曨垰鍐€妞ゆ劦婢€濞岊亪姊虹紒妯诲蔼闁稿海鏁诲濠氭晲婢跺﹤宓嗛梺缁樺姈缁佹挳宕戦幘璇叉嵍妞ゆ挻绋戞禍鐐叏濡厧浜鹃悗姘炬嫹

核心提示: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{
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"); //1,抛出异常
}catch ( Exception ex) {
System.out.println("No sUCh file found"); //2.catch捕捉异常,并执行
return -1; //4,return 返回
}finally{
System.out.println("Doing finally"); //3.finally一定会执行,在return之前。
}
return 0;
}
}
运行结果:
C:\java>java Mine
No such file found
Doing finally
-1
[]
更多精彩
赞助商链接