My question is lets say you have a class name BinarySearchTree code below. There
ID: 3760204 • Letter: M
Question
My question is lets say you have a class name BinarySearchTree code below. Theres also a node.java but that part is not important in this question. I keep getting an error in throw new ItemNotFoundException(); How to make ItemNotFoundException in my main.java program? any help will be appreciated.
public class BinarySearchTree<AnyType extends Comparable<? super AnyType>>
{
public void removeMin()
{
root= removeMin(root);
}
//removeMin
protected BinaryNode<AnyType>
removeMin(BinaryNode<AnyType> t)
{
if(t == null)
{
throw new ItemNotFoundException();
}
else if(t.left != null)
{
t.left= removeMin(t.left) ;
return t;
}
else
return t.right;
}
//root
protected BinaryNode<AnyType> root;
}//end class BinarySearchTree
Explanation / Answer
Its a built-in exception in java. Just trace the line in which you are getting that kind of exception, and add the phrase
"throws ItemNotFoundException" to the function that contains the line. For example, when you try to remove a minimum node using the function, removeMin(), and the tree is empty, this can lead to that exception. So, when defining that function, we'll simply define it like:
public void removeMin() throws ItemNotFoundException
{
}
With this statement, when you tried to remove a node that doesn't exist, it will throw an exception and will be handled accordingly.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.