黑马程序员技术交流社区

标题: 异常捕获处理问题(java 7) [打印本页]

作者: 王红潮    时间: 2012-9-11 12:13
标题: 异常捕获处理问题(java 7)
本帖最后由 王红潮 于 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"); //这里可以正常赋值
}



作者: 程振    时间: 2012-9-13 09:17
  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则是可以的。
作者: 杨卫腾    时间: 2012-9-13 09:48
   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();  不兼容类型  
作者: 柳彬    时间: 2012-9-13 10:08
这是因为你程序捕获的异常不是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




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2