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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王红潮 中级黑马   /  2012-9-11 12:13  /  1456 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王红潮 于 2012-9-11 12:14 编辑
  1. public class DivTest2 {
  2. public static void main(String[] args) {
  3. try {
  4. int a = Integer.parseInt(args[0]);
  5. int b = Integer.parseInt(args[1]);
  6. int c = a / b;
  7. System.out.println("结果为:" + c);
  8. } catch (IndexOutOfBoundsException|NumberFormatException|ArithmeticException e) {
  9. System.out.println("数组越界、格式异常、算术异常之一");
  10. e = new ArithmeticException("test");//为什么不能对变量重新赋值
  11. }catch(Exception e){
  12. System.out.println("未知异常");
  13. e = new RuntimeException("test"); //这里可以正常赋值
  14. }


复制代码
catch (IndexOutOfBoundsException|NumberFormatException|ArithmeticException e) {
System.out.println("数组越界、格式异常、算术异常之一");
e = new ArithmeticException("test");//为什么不能对变量重新赋值

System.out.println("未知异常");
e = new RuntimeException("test"); //这里可以正常赋值
}


3 个回复

倒序浏览
  1. java.lang.Object
  2.    java.lang.Throwable
  3.         java.lang.Exception
  4.              java.lang.RuntimeException
  5.                     java.lang.ArithmeticException
复制代码
你这是类型转换问题,看上面的继承结构:ArrayIndexOutOfBoundsException,ArithmeticException等等,这几个异常类是都是RuntimeException的子类,子类A赋予子类B是不对了,所以第一个catch不对。而第二个catch是把子类RuntimeException赋予父类Exception则是可以的。
回复 使用道具 举报
   e = new ArithmeticException("test");
{
        public static void main(String[] args)
        {
                try
                {
                        int a = Integer.parseInt(args[0]);
                        int b = Integer.parseInt(args[1]);
                        int c = a / b;
                        System.out.println("结果为:" + c);
                }
                catch (IndexOutOfBoundsException|NumberFormatException|ArithmeticException e)
                {
                        System.out.println("数组越界、格式异常、算术异常之一");
                        e = new ArithmeticException("test");
                }
                catch(Exception e)
                {
                        System.out.println("未知异常");
                        e = new RuntimeException("test"); //这里可以正常赋值
                }
        }
}

[/code]

   为什么不能对变量重新赋值?[code]public class DivTest2 ?

   我想原因是这样的,ArithmeticException、IndexOutOfBoundsException是RuntimeException的子类,
  NumberFormatException要是RuntimeException的子类,这样的话就出现了,子类的引用指向了其他子类对象的引用,这个就错误了。
  
  这就相当于 class Cat extends Animal{}  class Dog extends Animal{}
   Cat c = new Cat();
  c = new Dog();  不兼容类型  
回复 使用道具 举报
这是因为你程序捕获的异常不是ArithmeticException异常,所以.e = new ArithmeticException("test");
错误,不管他是IndexOutOfBoundsException|NumberFormatException|ArithmeticException 中的哪个他们都
是RuntimeException的子类。所以子类是可以赋给父类的。如果捕获的事ArithmeticException时
10.e = new ArithmeticException("test");可以赋值
代码证明:
import java.lang.*;
public class DivTest2 {

public static void main(String[] args) {

try {

int a =3;

int b =0;

int c = a / b;

System.out.println("结果为:" + c);

}
catch (ArithmeticException e){
e = new ArithmeticException("test");
System.out.println("算术异常");
System.out.println(e.toString());
//为什么不能对变量重新赋值

}catch(Exception e){

System.out.println("未知异常");

e = new RuntimeException("test"); //这里可以正常赋值
System.out.println(e.toString());
}
}
}
运行结果:
算术异常
java.lang.ArithmeticException :test
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马