【发布时间】:2014-06-25 14:04:57
【问题描述】:
我创建了一个类Exception(知道已经存在同名的内置子类)如下:
class Exception {
public static void main(String args[]) {
int i=8;
try{
int a = args.length;
int g = i/a;
} catch(ArithemticException e){
System.out.println("here is the error:"+e);
}
}
}
class Except7 {
static int h;
public static void main(String args[]) {
try{
int h = 9 / 0;
System.out.println("//");
} catch(ArithmeticException e) {
h = 4;
System.out.println(h);
} catch(Exception e) {
System.out.println("h");
}
}
}
您可能已经注意到,我在之前的代码中写了错误的“算术”拼写,所以编译器肯定会显示一个错误,它无法找到这个符号 (ArithemticException e)。
我所做的是,我没有在这里更改拼写,而是编写了另一个代码(后者)。在这里,我写了算术的正确拼写并尝试编译它。它给了我两个错误:
incompatible types (required : Throwable found: Exception )cannot find symbol(ArithemticException.java)
我的疑问是:
我们怎么能这么容易地改变一个内置类(异常)?是因为它是一个抽象类吗?
即使我可以直接访问它(即不使用“import”语句),我也不倾向于更改异常的名称 (AritheticException)。我只是把它放在一个'catch'声明中。那么,为什么其他没有'import'语句的程序能够访问它。 (我也没有创建任何包,说它们是包保护的)
之前的程序没有被编译,还有其他程序出现“找不到符号”的错误。
【问题讨论】:
标签: java