拿自定义异常没有办法,写的代码还有错
package untitled5;
import java.lang.*;
class NonEvenNumberException
extends Exception {
public NonEvenNumberException(String message) {
super("无偶数异常");
}
}
class SameIntReturnException
extends Exception {
public SameIntReturnException(String message) {
super("返回的结果相同异常");
}
}
public class CustomException {
public CustomException() {
}
public static void main(String[] args) {
CustomException customexception = new CustomException();
int i;
int result;
int[] divisor = {
4, 8, 15, 32, 64, 127, 256, 512};
int[] dividend = {
2, 1, 2, 4, 4, 4, 8};
for (i = 0; i < divisor.length; i++) {
try {
result = divisor[i] / dividend[i];
if ( (result % 2) != 0) {
throw new NonEvenNumberException();
}
if (dividend[i] == 1) {
throw new SameIntReturnException();
}
}
catch (ArrayIndexOutOfBoundsException exc) {
System.out.println("没有匹配的数据");
}
catch (NonEvenNumberException en) {
System.out.println(en);
}
catch (SameIntReturnException es) {
System.out.println(es);
}
}
}
}
在红字处总是提示"CustomException.java": cannot find symbol; symbol : constructor NonEvenNumberException(), location: class untitled5.NonEvenNumberException at line 36, column 17
"CustomException.java": cannot find symbol; symbol : constructor SameIntReturnException(), location: class untitled5.SameIntReturnException at line 39, column 17
这个我总是改不出来,哪位大大帮个忙啊!
----------------解决方案--------------------------------------------------------
你的错误信息不是写得很详细了吗?
找不到无参的构造函数,你定义这两个异常的时候,只定义了有参的构造函数,所以你的程序通过不了编译
要改很简单,把throw new NonEvenNumberException();改成throw new NonEvenNumberException("异常");就可以了
----------------解决方案--------------------------------------------------------
啊!!!是啊!这么简单的问题竟然不会了!谢谢斑竹的指导啊!
----------------解决方案--------------------------------------------------------
是啊,定义了有参的构造函数后,就不能调用无参构造函数.
----------------解决方案--------------------------------------------------------