用Java创建用户定义的错误;为什么此代码不起作用?

禁食
import java.util.*;
class ArrayOverflowException extends Exception{
    ArrayOverflowException(){
        super(); 
    }
    static int i;
    static void check(int i) throws ArrayOverflowException{
        if (i>5) {
            throw new ArrayOverflowException("Array Overflow");
        return;
}
}
public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    int a[]=new int[5];
    System.out.println("Enter the elemets of the array. #MAX 6 ELEMENTS#");
    try{
        for (int i=0;i<10;i++) {
            ArrayOverflowException.check(i);
            a[i]=input.nextInt();
        }
    }catch (ArrayOverflowException e) {
        System.out.println("Exception handled"+e);
    }
}
}

我一直试图在Java中创建自己的用户定义异常,该异常给出了错误cannot find symbol in line 9请帮忙。

第9行抛出新的ArrayOverflowException(“ Array Overflow”);

奥莱西亚

1)使用字符串参数添加构造函数

ArrayOverflowException(String message){
    super(message); 
}

2)删除返回码,这是无法访问的代码

return;

还检查您是否要检查i或用户输入的值?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章