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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 余银桂 中级黑马   /  2012-6-18 19:13  /  1439 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //题目一
  2. public class TryCatch {

  3.         public static void main(String[] args) {
  4.                 System.out.println("main "+new TryCatch().test());
  5.         }
  6.         
  7.         public int test(){
  8.                 int i = 1;
  9.         try
  10.         {
  11.                 i = 55555;
  12.                    return i;
  13.         } catch(Exception e) {
  14.                 return i;
  15.         }
  16.         
  17.         finally
  18.         {
  19.             i = 1000;
  20.             
  21.            
  22.         }

  23.         }
  24. }
复制代码
  1. //题目二

  2. public class TryCatch {

  3.         public static void main(String[] args) {
  4.                 System.out.println("main "+new TryCatch().test());
  5.         }
  6.         
  7.         public int test(){
  8.                 int i = 1;
  9.         try
  10.         {
  11.                 i = 55555;
  12.                    return i;
  13.         } catch(Exception e) {
  14.                 return i;
  15.         }
  16.         
  17.         finally
  18.         {
  19.             i = 1000;
  20.             return i;
  21.             
  22.            
  23.         }

  24.         }
  25. }




  26. //尝试一下一眼看过去 你认为的答案是什么,然后运行验证一下
复制代码

评分

参与人数 1黑马币 +20 收起 理由
黄奕豪 + 20 赞一个!

查看全部评分

6 个回复

倒序浏览
输出 main 55555.。。楼主要说明说明问题呢?
回复 使用道具 举报
胡大强 发表于 2012-6-18 19:25
输出 main 55555.。。楼主要说明说明问题呢?

2个都是么?
回复 使用道具 举报
余银桂 发表于 2012-6-18 19:56
2个都是么?

你定义的test()方法的返回类型是int型,第一个finally执行了但是没有return i,所以打印的还是main 5555
第二个执行了finally后将i=1000返回给main方法打印出来了、finally是必须执行的。
回复 使用道具 举报
mark           
回复 使用道具 举报
mark                        
回复 使用道具 举报
还好全中
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马