1

I'm currently working on a project where I have to make a binary search tree and I'm having a little problem implementing my getRootData method

I've already tried importing different things or trying to find an implemented method but I haven't found any luck.

public T getRootData() {
    if (isEmpty())
        throw new EmptyTreeException();
    else
        return root.getData();
}

Am I supposed to write a class for EmptyTreeExcpetion or is there something else I need to do.

  • 2
    It's not clear to me what the problem/question is. – lealceldeiro Apr 20 '19 at 19:44
  • @lealceldeiro my IDE keeps telling me " cannot resolve symbol 'EmptyTreeException' " – Paul Anthony Morillo Apr 20 '19 at 19:51
  • @PaulAnthonyMorillo have you written `EmptyTreeException` class ? – Zain Arshad Apr 20 '19 at 19:59
  • @ZainArshad no i have not, I was wondering if that is what I am supposed to do – Paul Anthony Morillo Apr 20 '19 at 20:02
  • `getRootData()` is not a class, it's a method. Hence it is part of a class. And because it returns `T`, I'm guessing that the class it is part of is a generic class. If you are only using the standard JDK classes along with classes that you wrote, then there is no `EmptyTreeException` class in the JDK, which means you have to write it. Do you know how to do that? – Abra Apr 20 '19 at 20:03
  • @Abra no im not sure how to write the EmptyTreeException, I wasn't sure if it was like the other exceptions that are default in the jdk. I have a textbook that just used EmptyTreeException but they didnt have any implementations of it – Paul Anthony Morillo Apr 20 '19 at 20:08
  • @PaulAnthonyMorillo you can test my answer if it helps or not ? – Zain Arshad Apr 20 '19 at 20:19

2 Answers2

2

So first you have to write a EmptyTreeException class as there is no such built-in class for this

public class EmptyTreeException extends RuntimeException
{   
    public EmptyListException()
    {
        super ("Tree is Empty");
    }
}

Now, you have to change your getRootData() like this:

public T getRootData() throws EmptyTreeException
{
    if (isEmpty())
        throw new EmptyTreeException();
    else
        return root.getData();
}

Edit: You don't have to change your getRootData(), as mentioned by fellow SO's users that EunTimeException is an "unchecked exception" so no need to do this throws EmptyTreeException after method header.

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
  • 2
    _Now, you have to change your `getRootData()` like this_ No he doesn't, because your implementation makes `EmptyTreeException` an **unchecked exception**, which seems logical since in the OP's code, method `getRootData()` does not declare that it throws `EmptyTreeException`. – Abra Apr 20 '19 at 20:12
  • @Abra you are right ... I just did what I used to do for years ... hahaaI understood it now :) – Zain Arshad Apr 20 '19 at 20:14
  • _wouldn't it give any error ?_ There's one way to find out ;-) – Abra Apr 20 '19 at 20:17
  • @lealceldeiro can you enlighten me about this being "nota good idea"..? any help is appreciated – Zain Arshad Apr 20 '19 at 20:25
  • Interesting, @lealceldeiro, a lot of people I come across tend to think _checked_ exceptions should be avoided at all costs. Regardless, you should extend the exception that makes sense. In this case, I don't see the benefit in making `EmptyTreeException` a checked exception—it's similar to `NoSuchElementException`, which is unchecked. In other words, if the tree is empty it's probably a _programming error_ rather than a _recoverable error_ caused by something outside the developers control. – Slaw Apr 20 '19 at 20:33
  • 3
    I agree with @Slaw and personally try to use unchecked exceptions in preference to checked ones, but I defer to Josh Bloch's book _Effective Java, Third Edition_, namely item 70: _Use checked exceptions for recoverable conditions and runtime exceptions for programming errors_. – Abra Apr 20 '19 at 20:36
0

Hi @PaulAnthonyMorillo,

There is not the class exception EmptyTreeException on any JDK. Then we need to create a class that extends Exception or RuntimeException to throw it.

We can verify if there is a class on java search on javadoc. Like javadoc on jdk11

I hope I have helped,

  • 1
    _There is not the class exception EmptyTreeException on jdk 11._ Please enlighten me because I didn't see any mention of JDK version being used in the original question. In any case, there has **never** been a `EmptyTreeException` in any JDK version. – Abra Apr 20 '19 at 20:20
  • Hi @Abra, I only check Exception on jdk11. I think it would be enough. But I did not know and did not check in others jdks. I will fix the answer. Thanks by feedback :) – Marcos Côrtes Apr 20 '19 at 20:57