简体   繁体   English

我无法自定义例外。 请帮我

[英]Can't make my own custom exception. Please help me

When I try and make a catch statement using InvalidTestScore exception java won't allow me. 当我尝试使用InvalidTestScore异常创建catch语句时,java将不允许我使用。 However when I use IllegalArgumentException, java does allow me to make it. 但是,当我使用IllegalArgumentException时,java确实允许我这样做。

// George Beazer

public class TestScores2 {
    public TestScores2(int[] arg) {
        System.out.println(average(arg));
    }
    public int average(int[]arg)
    {
        int temp=0;
        for (int i = 0; i < arg.length; i++) {
            if(arg[i]<0 || arg[i]>100)
            {
                IllegalArgumentException e = new IllegalArgumentException();
                throw e;
            }
            else
            {
                temp+=arg[i];
            }
        }
        return temp/arg.length;
    }
    public static void main(String[] args) {
        int []ar={4,78,33,89};
        TestScores2 ts=new TestScores2(ar);
    }

}

Runs fine 运行良好

However if i run 但是如果我跑

public class TestScores2 {
    public TestScores2(int[] arg) {
        System.out.println(average(arg));
    }
    public int average(int[]arg)
    {
        int temp=0;
        for (int i = 0; i < arg.length; i++) {
            if(arg[i]<0 || arg[i]>100)
            {
                InvalidTestScoreException e = new InvalidTestScore();
                throw e;
            }
            else
            {
                temp+=arg[i];
            }
        }
        return temp/arg.length;
    }
    public static void main(String[] args) {
        int []ar={4,78,33,89};
        TestScores2 ts=new TestScores2(ar);
    }

}

I get can't find symbol. 我得到找不到符号。 What does it take to make your own custom exception . 创建自己的自定义异常需要什么?

InvalidTestScoreException e = new InvalidTestScore();

??? ??? shouldn't that be: 不应该是:

InvalidTestScoreException e = new InvalidTestScoreException();

As @Falmarri pointed out, you need to declare an InvalidTestScoreException class 正如@Falmarri指出的那样,您需要声明一个InvalidTestScoreException类

Here is what a revised version might look like: 修改后的版本如下所示:

public class TestScores2 {

    public class InvalidTestScoreException extends RuntimeException {
        //Constructors go here
    }

    public TestScores2(int[] arg) {
        System.out.println(average(arg));
    }
    public int average(int[]arg)
    {
        int temp=0;
        for (int i = 0; i < arg.length; i++) {
            if(arg[i]<0 || arg[i]>100)
            {
                InvalidTestScoreException e = new InvalidTestScoreException();
                throw e;
            }
            else
            {
                temp+=arg[i];
            }
        }
        return temp/arg.length;
    }
    public static void main(String[] args) {
        int []ar={4,78,33,89};
        TestScores2 ts=new TestScores2(ar);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的链表(带有泛型)程序可以很好地编译,但是当我运行它时,却出现未找到类的异常。 有人能帮我吗? - My Linked List (with generics) program compiles fine, but when I run it I am getting a class not found exception. Can someone help me? 在我的JSP中找不到使用jQuery的方法,请帮帮我 - Can't find a way to use jQuery in my JSP, help me please cxf与轴,类强制转换异常。 我可以声明自己的类加载器以避免 - cxf with axis, class cast exception. Can I declare my own classloader to avoid 无法获取异常原因。 谁能给我提示? 空值在哪里? - Can't get the reason of the exception. Can anyone give me the clue? Where is the null there? 为什么会发生此异常,请帮帮我 - why this exception occurs please help me 请帮助我解决这个JAVA中的异常 - Please help me sort out this Exception In JAVA 请帮助我解决代码中的非法monitorstate异常 - Please help me to resolve illegalmonitorstate exception in the code 我不知道为什么我的程序给我花括号错误。 我怎样才能解决这个问题 ? 请帮忙 - I don't know why my program is giving me curly brace errors. How can I fix this ? Please help 请帮助解决此空指针异常。 该项目涉及struts2,jsp,java和struts.xml - Please help in resolving this null pointer exception. This projct involves with struts2, jsp, java and struts.xml 请在我的AsyncTask类中帮助我 - please help me in my AsyncTask class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM