java 如果添加的对象有重复,如何抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35567328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:17:37  来源:igfitidea点击:

how to throw an exception if the object that is being added has a duplicate

java

提问by Wahegurupal Singh

I have to add a new object to an array but throw an exception if that object already exists.I don't know what to add after 'throw___________;'. I have made the object class and also a class to hold an array of that object.I have also done the part where i have to add it to the array but i don't know what exception to throw if the object already exists in that array.

我必须向数组添加一个新对象,但如果该对象已存在,则抛出异常。我不知道在“throw_______;”之后要添加什么。我已经创建了对象类和一个类来保存该对象的数组。我还完成了必须将其添加到数组的部分,但我不知道如果该对象已存在于该数组中,我不知道要抛出什么异常大批。

回答by josivan

The straightforward to do is throw an existent exception. You can do something like.

最简单的方法是抛出一个存在的异常。你可以做类似的事情。

throw new IllegalArgumentException();

Or use the constructor with String parameter

或者使用带有 String 参数的构造函数

throw new IllegalArgumentException("The value is already in the list.");

You can see the documentation of IllegalArgumentExceptionon oracle website.

您可以在 oracle 网站上查看IllegalArgumentException的文档。

If you prefer to use a custom exception. You need to follow the suggestion of @3kings. But you have to user the newoperator. For example, throw new MyCustomeException().

如果您更喜欢使用自定义异常。您需要遵循@3kings 的建议。但是您必须使用new运营商。例如,throw new MyCustomeException()

回答by Sriniketh

Create a custom exception classlike the one below:

创建一个自定义异常class,如下所示:

public class CustomException extends Exception
{

private static final long serialVersionUID = 1997753363232807009L;

    public CustomException()
    {
    }

    public CustomException(String message)
    {
        super(message);
    }

    public CustomException(Throwable cause)
    {
        super(cause);
    }

    public CustomException(String message, Throwable cause)
    {
        super(message, cause);
    }

    public CustomException(String message, Throwable cause, 
                                       boolean enableSuppression, boolean writableStackTrace)
    {
        super(message, cause, enableSuppression, writableStackTrace);
    }

}

You can use it as follows:

您可以按如下方式使用它:

throw new CustomException("blah blah blah");

Refer this link: http://examples.javacodegeeks.com/java-basics/exceptions/java-custom-exception-example/

请参阅此链接:http: //examples.javacodegeeks.com/java-basics/exceptions/java-custom-exception-example/

回答by ifly6

Create a RuntimeExceptionby making a new class that extends RuntimeException. Or, you could just use a Setrather than an array. Thus, you would get automatic duplicate-checking.

通过创建一个RuntimeException扩展RuntimeException. 或者,您可以只使用 aSet而不是数组。因此,您将获得自动重复检查。