0

I want to add an element to a BinarySearchTree. I have a condition that checks if the element is already in the tree and if it is I want to throw an exception. My problem is that I do not know what type, or what the name of this exception is. I was looking for DublicateItemException but it does not work. I am working with java. Any ideas? Thanks

0

3 Answers 3

1

The Java TreeSet simply returns false when you try to add an already-present element to the tree, but Queue sets an alternative precedent of throwing an IllegalStateException. There's no exception built into Java for the case you're looking for, though.

Sign up to request clarification or add additional context in comments.

Comments

1

You can create your own exception for it.

Just create a new class DuplicateItemException, and make sure it extends Exception:

public static class DuplicateException extends Exception {

    private static final long serialVersionUID = 6188088059604835525L; //change the number, if needed - was auto generated by eclipse

}

If you want to use an existing class - maybe IllegalArgumentException might fit, though not perfectly.

Comments

1

Why not create your own?

public class DuplicateItemException extends Exception
{
}

1 Comment

I do not know how to create a class for an exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.