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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© haohanlinyu 中级黑马   /  2014-6-7 17:10  /  1210 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 haohanlinyu 于 2014-6-10 23:01 编辑

package com.itheima;
/*6、 用代码证明,在try中写了return,后面又写了finally,是先执行return还是先执行fianlly?*/
public class Test6
{   
        
        public static void main(String[] args) throws Exception
        {
                Object obj;
                sop(firstrun(obj));
        }
        public static firstrun(Object obj) throws Exception
        {
                int x=1;
                try
            {
                return x;        
            }
            finally
            {
                    return ++x;
            }
        }
        public static void sop(Object obj)
        {
        System.out.print(obj);
        }
}

8 个回复

倒序浏览
haohanlinyu 发表于 2014-6-7 18:22
谢谢你  我已经按照你的提示  改对了  非常感谢

:D不客气~
加油!
回复 使用道具 举报
public static firstrun(Object obj) throws Exception 这行代码的返回值类型 你没有声明吧
回复 使用道具 举报
本帖最后由 李小然 于 2014-6-7 18:27 编辑

一共两处错误:
1.Object obj; obj为局部变量,必须先初始化
2.public static firstrun(Object obj) 既然return x(int)
  则要声明为public static int firstrun(Object obj)

评分

参与人数 1技术分 +1 收起 理由
轻语。 + 1

查看全部评分

回复 使用道具 举报 1 0
李小然 发表于 2014-6-7 18:02
一共两处错误:
1,Object obj; obj为局部变量,必须先初始化
2.public static firstrun(Object obj) 既然r ...

谢谢你  我已经按照你的提示  改对了  非常感谢
回复 使用道具 举报
我很奇怪  你既然try了   为什么还要throws
回复 使用道具 举报
你那个无法证明先返回return  还是  finnaly  看看我这个吧!
  1. public class Test5 {
  2.         public static void main(String[] args) {
  3.                 try{
  4.                         System.out.println("return");
  5.                         return;
  6.                 }
  7.                 finally{
  8.                        
  9.                         System.out.println("finnally");
  10.                 }
  11.         }

  12. }
复制代码
回复 使用道具 举报
、海 发表于 2014-6-7 19:08
我很奇怪  你既然try了   为什么还要throws

没有 catch呀 。当然得throws
回复 使用道具 举报
、海 发表于 2014-6-7 19:28
你那个无法证明先返回return  还是  finnaly  看看我这个吧!

你这个return的是什么呀??
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马