《java语言基础教程》(朱福喜)
p296
Console 是 final 类呀 怎么可以坐父类呢?
public class ShortStringReader extends Console {
private final static int MAX = 10;
public ShortStringReader() throws LongStringException{
// TODO Auto-generated constructor stub
if(super.readString().length() <= MAX)
return super.readString();
else
throw new LongStringException("String to long. MAX:" + MAX);
}
}p300在Quotient()中已经catch到了ArithmeticException 干吗还throws ArithmeticException 呢public class Main {
static void Quotient(int n, int d){// throws ArithmeticException {
try {
System.out.println(n/d);
} catch (ArithmeticException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
int numer = 10;
int denom = 0;
Quotient(numer, denom);
}
}
p296
Console 是 final 类呀 怎么可以坐父类呢?
public class ShortStringReader extends Console {
private final static int MAX = 10;
public ShortStringReader() throws LongStringException{
// TODO Auto-generated constructor stub
if(super.readString().length() <= MAX)
return super.readString();
else
throw new LongStringException("String to long. MAX:" + MAX);
}
}p300在Quotient()中已经catch到了ArithmeticException 干吗还throws ArithmeticException 呢public class Main {
static void Quotient(int n, int d){// throws ArithmeticException {
try {
System.out.println(n/d);
} catch (ArithmeticException e) {
System.out.println(e);
}
}
public static void main(String[] args) {
int numer = 10;
int denom = 0;
Quotient(numer, denom);
}
}
第二个的确不需要throws ArithmeticException
第二个catch了就不能再扔出来吗 catch到再往外扔
Java里没这个的, 估计是作者自定义的吧
第2个确实多余,不过声明不会报错的
好久没碰Java了 呵呵
System.out.println(e);
}
已经处理 e 了,ArithmeticException 根本抛不出来。
除非这样
catch (ArithmeticException e) {
System.out.println(e);
throw e;
}
不过
// throws ArithmeticException
不是已经注释掉了么?第一个的话,Console应该不是java.io.Console
我看到的 java.io.Console 是jdk 1.6的。
而且,上面的Console调用了super.readString();
可是java.io.Console根本没有readString()这个方法。
第二个 看来是作者画蛇添足了