边学边跟着写了如下代码:
class D {
public int div(int a, int b) {//第二行
if(0==b)
throw new ArithmeticException("000000000");
return a/b;
}
}
class A {
public static void main(String[] args) {
D d = new D();
int x = d.div(4,0);
}
}
class ArithmeticException extends Exception {
ArithmeticException(String msg) {
super(msg);
}
}
class D {
public int div(int a, int b) throws ArithmeticException {
if(0==b)
throw new ArithmeticException("000000000");
return a/b;
}
}
class A {
public static void main(String[] args) {
D d = new D();
try {
int x = d.div(4,0);
}
catch (ArithmeticException e) {
System.out.println(e.toString());
}
}
}
相当于自己重写了一个ArithmeticException类
这样一来,在源目录下就产生了一个ArithmeticException.class的文件,然后在编译一楼的代码时,就直接在同目录下找到了这个文件,结果就报错啦!!
嘿嘿,算是累积了一点经验吧,共勉之!!!
作者: 索学超 时间: 2012-4-25 18:32
//代码:
class D{
public int div(int a ,int b)
{
if(0==b)
throw new ArithmeticException("0000000000");
return a/b;
}
}
class A {
public static void main(String[] args)
{
D d = new D();
int x = d.div(4, 0);
System.out.println(x);
}
}
//输出结果
Exception in thread "main" java.lang.ArithmeticException: 0000000000
at D.div(A.java:7)
at A.main(A.java:16)
这算通过不
如果算的话,可能是你没弄好吧