class OutBoundsException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L; OutBoundsException(String mes){
super(mes);
}
}
class Check{
String ChecktheNum(int n) throws OutBoundsException{
Integer N=new Integer(n);
if(n>30||n<20)
throw new OutBoundsException("the number is out of bound!!");
else
return "the number"+N.toString()+"is in the bounds";
}
}
class Test {
public static void main(String args[]) {
Check c=new Check();
try{
System.out.println("以下是合法的数据报告:");
System.out.println(c.ChecktheNum(25));
System.out.println("以下是非法的数据报告:");
System.out.println(c.ChecktheNum(5));
}catch(OutBoundsException e){
System.out.println(e.toString());
}}
}return "the number"+N.toString()+"is in the bounds";中的N.toString()怎么解释 ??还有下面的 System.out.println(e.toString());是什么意思啊 为什么是e.toString()而不是c.toString()???? 急啊 !!!谢谢大虾们了
/**
*
*/
private static final long serialVersionUID = 1L; OutBoundsException(String mes){
super(mes);
}
}
class Check{
String ChecktheNum(int n) throws OutBoundsException{
Integer N=new Integer(n);
if(n>30||n<20)
throw new OutBoundsException("the number is out of bound!!");
else
return "the number"+N.toString()+"is in the bounds";
}
}
class Test {
public static void main(String args[]) {
Check c=new Check();
try{
System.out.println("以下是合法的数据报告:");
System.out.println(c.ChecktheNum(25));
System.out.println("以下是非法的数据报告:");
System.out.println(c.ChecktheNum(5));
}catch(OutBoundsException e){
System.out.println(e.toString());
}}
}return "the number"+N.toString()+"is in the bounds";中的N.toString()怎么解释 ??还有下面的 System.out.println(e.toString());是什么意思啊 为什么是e.toString()而不是c.toString()???? 急啊 !!!谢谢大虾们了
e.toString() 表示将异常转换成String类型方便输出
2、catch(OutBoundsException e){
System.out.println(e.toString());
}
e.toString()是接受抛出的异常e信息。
String toString()
返回一个表示该 Integer 值的 String 对象。
Throwable的toString方法(Exception继续Throwable)
public String toString()返回此 throwable 的简短描述。结果是以下字符串的串联:
此对象的类的 name
": "(冒号和一个空格)
调用此对象 getLocalizedMessage() 方法的结果
如果 getLocalizedMessage 返回 null,则只返回类名称。
看完这2个API你就知道是什么意思了,请常备API,不懂及时查看,然后直接F3跳到代码里面去看看jdk是怎么实现的。
N为Integer类型 public String toString() {
return String.valueOf(value);
}
2,因为你catch(OutBoundsException e) 扑捉的是e 所以是是e.toString()而不是c.toString()
再弱弱的问一下啊 N.toString()去掉toString()和不去掉的打印结果一样 为什么啊??写上和不写上有区别吗??麻烦了啊
N.toString()是String类型 N是Integer类型
尽管System.out.println的输出结果一样,但这是System.out.println的作用,不能说明写与不写toString()没有区别System.out是PrintStream类型,可以看他的println的代码 public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
} public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}也即,输出时是先将Integer转换成String s 然后输出
"the number"+N.toString()+"is in the bounds";
+号前后连接的类型是 字符串+()+字符串。当然里面要转换成字符串形式喽。
而 N是Integer类型,toString转换成N的String类型数值。
而后的
catch(OutBoundsException e){
System.out. println(e.toString());
}
对象c是局部的 只能在他自己的大括号内,e.toString输出抛出异常的信息,e是OutBoundsException 的对象,