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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 尹博 中级黑马   /  2012-4-11 19:19  /  2594 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


  1. class MutiException
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 try
  6.                 {
  7.                 System.out.println(div(4,1));
  8.                 }
  9.                 catch(ArithmeticException e)
  10.                 {
  11.                         System.out.println("haha" + e.toString());
  12.                 }
  13.                 catch(ArrayIndexOutOfBoundsException e)
  14.                 {
  15.                         System.out.println("heihei" + e.toString());
  16.                 }
  17.                
  18.         }
  19.         static int div(int a,int b) throws Exception //为什么不能这么写?
  20.         {
  21.                 int[] arr = new int[a];
  22.                 System.out.println(arr[3]);
  23.                 return a / b;
  24.         }
  25. }

复制代码
编译时说:错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

11 个回复

倒序浏览
本帖最后由 邓斌 于 2012-4-11 19:43 编辑

不好意思我错了。回头看了下笔记
ArithmeticException,
ArrayIndexOutOfBoundsException

by0 和数组角标越界异常都是小弟。


Throwable //所有错误信息的超类,
        —-Error
        --Exception

你抛出的是一个三大王。。而catch接收不鸟。我只能请小弟进来。老大进来太大了。

而如果。你抛的是ArithmeticException, ArrayIndexOutOfBoundsException。 catch节后的是Exception 这样是可行的。多态特性。


1。声明异常时,建议声明更为具体的异常。这样处理的可以更具体。
2。对方声明几个异常。就对应有几个catch块。进行针对性处理。不要定义多余的catch块
        如果多个catch块中的异常出现继承关系。父类catch块放在最下面。

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

回复 使用道具 举报
  int[] arr = new int[a];  数组不能接收一个变量来初始化它的长度吧
回复 使用道具 举报
程旦 发表于 2012-4-11 19:39
int[] arr = new int[a];  数组不能接收一个变量来初始化它的长度吧

这里可以,在函数运行时值就传进来了,好像c语言中不可以,不怎么记得了
回复 使用道具 举报
你都throws Exception了,说明你这程序可能会出现问题,怎么能不捕获处理呢? 要
try{}

catholic()
{}

回复 使用道具 举报
邓斌 发表于 2012-4-11 19:29
不好意思我错了。回头看了下笔记
ArithmeticException,
ArrayIndexOutOfBoundsException

差点被你带进去了,呵呵,后来突然想到这些异常都是已知的,不需要在内部抛。
其实我没问太具体。当我把Exception改成ArrayIndexOutOfBoundsException、ArithmeticException的直接父类RuntimeException时就可以了,这样我觉得是不是因为Exception的直接子类中没有这两个异常的缘故?
回复 使用道具 举报
尹博 中级黑马 2012-4-11 19:51:51
7#
石好强 发表于 2012-4-11 19:48
你都throws Exception了,说明你这程序可能会出现问题,怎么能不捕获处理呢? 要
try{}

try了呢,呵呵
回复 使用道具 举报
  div()方法的异常抛到main方法里面去了,而mian你又没进行捕获异常。 要么你main方法里面捕获,要么也抛出去

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

回复 使用道具 举报
邓斌 中级黑马 2012-4-11 20:08:33
9#
尹波 发表于 2012-4-11 19:50
差点被你带进去了,呵呵,后来突然想到这些异常都是已知的,不需要在内部抛。
其实我没问太具体。当我把E ...

呵呵。如果RuntimeException可以的话。可能就是你想的那样吧。。RuntimeException 本来就特殊。不需要处理直接停止程序。
回复 使用道具 举报
class MutiException
{
        public static void main(String[] args)
        {
                try
                {
                System.out.println(div(4,1));//未处理异常
                }
                catch(ArithmeticException e)
                {
                        System.out.println("haha" + e.toString());
                }   

                catch(ArrayIndexOutOfBoundsException e)
                {
                        System.out.println("heihei" + e.toString());
                }     
                catch(Exception e)
                {
                                System.out.println("hehe" + e.toString());
                }

               
        }
        static int div(int a,int b) throws Exception //为什么不能这么写?
        {
                int[] arr = new int[a];
                System.out.println(arr[3]);
                return a / b;
        }
}

你抛的Exception,main函数里面没有抛出也没有处理。处理的都是所属于Exception子类异常。那些异常不能完全处理Exception  也就是你万一出一个Exception其它子类的异常,你无法处理。 你必须在最后做一次Exception处理。

评分

参与人数 1技术分 +1 收起 理由
岳民喜 + 1

查看全部评分

回复 使用道具 举报
常万 发表于 2012-4-11 20:18
class MutiException
{
        public static void main(String[] args)

哎,考虑不周啊,感谢~
回复 使用道具 举报
尹波 发表于 2012-4-11 20:29
哎,考虑不周啊,感谢~

级别问题,都在学习中:handshake
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马