Java程序異常處理的特殊情況

字號(hào):

1、不能在finally塊中執(zhí)行return,continue等語(yǔ)句,否則會(huì)把異?!俺缘簟?
    2、在try,catch中如果有return語(yǔ)句,則在執(zhí)行return之前先執(zhí)行finally塊
    請(qǐng)大家仔細(xì)看下面的例子:
    以下是引用片段:
    public class TryTest {
     public static void main(String[] args) {
     try {
     System.out.println(TryTest.test());// 返回結(jié)果為true其沒(méi)有任何異常
     } catch (Exception e) {
     System.out.println("Exception from main");
     e.printStackTrace();
     }
     doThings(0);
     }
     public static boolean test() throws Exception {
     try {
     throw new Exception("Something error");// 第1步.拋出異常
     } catch (Exception e) {// 第2步.捕獲的異常匹配(聲明類或其父類),進(jìn)入控制塊
     System.out.println("Exception from e");// 第3步.打印
     return false;// 第5步. return前控制轉(zhuǎn)移到finally塊,執(zhí)行完后再返回(這一步被吃掉了,不執(zhí)行)
     } finally {
     return true; // 第4步. 控制轉(zhuǎn)移,直接返回,吃掉了異常
     }
     }
     public static void doThings(int i)
     {
     try
     {
     if(i==0)
     {
     //在執(zhí)行return之前會(huì)先執(zhí)行finally
     return;
     }
     int t=100/i;
     System.out.println(t);
     }catch(Exception ex)
     {
     ex.printStackTrace();
     }
     finally
     {
     System.out.println("finally");
     }
     }
    }