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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 荣天 中级黑马   /  2012-5-19 10:45  /  1420 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

第一个:
  1. /*
  2. * 写出程序结果
  3. */
  4. package ExceptionDemo;
  5. public class ExceptionDemo9
  6. {
  7.         public static void jun()
  8.         {
  9.                 try
  10.                 {
  11.                         throw new Exception();
  12.                 }
  13.                 finally
  14.                 {
  15.                         System.out.println("finally");
  16.                 }
  17.         }
  18.         public static void main(String[] args) {
  19.                 try
  20.                 {
  21.                         jun();
  22.                         System.out.println("MAIN");
  23.                 }
  24.                 catch(Exception e )
  25.                 {
  26.                         System.out.println("catch");
  27.                 }
  28.                 System.out.println("结束");
  29.         }
  30. }


  31.        


复制代码
第二个:
  1. /*
  2. * 写出程序结果
  3. */
  4. package ExceptionDemo;
  5. public class ExceptionDemo9
  6. {
  7.         public static void jun()throws Exception
  8.         {
  9.                 try
  10.                 {
  11.                         throw new Exception();
  12.                 }
  13.                 finally
  14.                 {
  15.                         System.out.println("finally");
  16.                 }
  17.         }
  18.         public static void main(String[] args) {
  19.                 try
  20.                 {
  21.                         jun();
  22.                         System.out.println("MAIN");
  23.                 }
  24.                 catch(Exception e )
  25.                 {
  26.                         System.out.println("catch");
  27.                 }
  28.                 System.out.println("结束");
  29.         }
  30. }


  31.        


复制代码
答案有点  接受不了   大家的答案??   赐教  谢谢

9 个回复

倒序浏览
只是第一个没有加  jun()函数后没有加throws Exception,会报错。
第二个加入,很好。
有什么疑问?
回复 使用道具 举报
第一个你要catch下异常或者声明抛出异常。。。
第二个结果为
finally
catch
结束

join()函数捕获异常  但是finally必须执行 所以先输出finally
抓住了异常进入catch语句 输出 catch
最后输出 结束

不知楼主明白没
回复 使用道具 举报
李哲 发表于 2012-5-19 11:09
只是第一个没有加  jun()函数后没有加throws Exception,会报错。
第二个加入,很好。
有什么疑问? ...

     谢   谢
回复 使用道具 举报
程旦 发表于 2012-5-19 11:25
第一个你要catch下异常或者声明抛出异常。。。
第二个结果为
finally

谢谢  :)
回复 使用道具 举报
李哲 发表于 2012-5-19 11:09
只是第一个没有加  jun()函数后没有加throws Exception,会报错。
第二个加入,很好。
有什么疑问? ...

jun()函数后没有加throws Exception
catch下 也可以吧??
回复 使用道具 举报
李哲 中级黑马 2012-5-19 11:55:00
7#
荣天 发表于 2012-5-19 11:50
jun()函数后没有加throws Exception
catch下 也可以吧??

对。我刚才没考虑这点。
catch,如果不再抛出新的异常,函数就不需要声明。

另外,抛出RuntimeException()是不需要声明的。

建议多看视频,加油。
回复 使用道具 举报
public class ExceptionDemo9

{

        public static void jun()throws Exception//未在函数上申明抛出异常
               

        {

               try

               {

                       throw new Exception();

                }

                finally

               {

                        System.out.println("finally");

               }

        }

        public static void main(String[] args) {

                try

                {

                        jun();
                        System.out.println("MAIN");

               }

                catch(Exception e )

               {

                      System.out.println("catch");

               }

               System.out.println("结束");

        }

}
回复 使用道具 举报
运行结果
finally
catch
结束
回复 使用道具 举报
李哲 发表于 2012-5-19 11:55
对。我刚才没考虑这点。
catch,如果不再抛出新的异常,函数就不需要声明。

好的   非常感谢:)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马