A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 不想飞不到 中级黑马   /  2014-8-9 12:31  /  994 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

[size=13.913043975830078px]写出结果:
  • public class TestException {  
  •     public TestException() {  
  •     }  
  •   
  •     boolean testEx() throws Exception {  
  •         boolean ret = true;  
  •         try {  
  •             ret = testEx1();  
  •         } catch (Exception e) {  
  •             System.out.println("testEx, catch exception");  
  •             ret = false;  
  •             throw e;  
  •         } finally {  
  •             System.out.println("testEx, finally; return value=" + ret);  
  •             return ret;  
  •         }  
  •     }  
  •   
  •     boolean testEx1() throws Exception {  
  •         boolean ret = true;  
  •         try {  
  •             ret = testEx2();  
  •             if (!ret) {  
  •                 return false;  
  •             }  
  •             System.out.println("testEx1, at the end of try");  
  •             return ret;  
  •         } catch (Exception e) {  
  •             System.out.println("testEx1, catch exception");  
  •             ret = false;  
  •             throw e;  
  •         } finally {  
  •             System.out.println("testEx1, finally; return value=" + ret);  
  •             return ret;  
  •         }  
  •     }  
  •   
  •     boolean testEx2() throws Exception {  
  •         boolean ret = true;  
  •         try {  
  •             int b = 12;  
  •             int c;  
  •             for (int i = 2; i >= -2; i--) {  
  •                 c = b / i;  
  •                 System.out.println("i=" + i);  
  •             }  
  •             return true;  
  •         } catch (Exception e) {  
  •             System.out.println("testEx2, catch exception");  
  •             ret = false;  
  •             throw e;  
  •         } finally {  
  •             System.out.println("testEx2, finally; return value=" + ret);  
  •             return ret;  
  •         }  
  •     }  
  •   
  •     public static void main(String[] args) {  
  •         TestException testException1 = new TestException();  
  •         try {  
  •             testException1.testEx();  
  •         } catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •     }  
  • }  




你的答案是下面这样么?


i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, catch exception
testEx, finally; return value=false



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马