FranXh
FranXh

Reputation: 4771

Binary Search Tree, adding the same element exception.

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

Upvotes: 0

Views: 224

Answers (3)

Diego
Diego

Reputation: 18379

Why not create your own?

public class DuplicateItemException extends Exception
{
}

Upvotes: 1

Louis Wasserman
Louis Wasserman

Reputation: 198481

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.

Upvotes: 1

amit
amit

Reputation: 178521

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.

Upvotes: 1

Related Questions