0

I have a method:

public List<Model> getModelList() throws ModelNotFoundException {
    if (modelList.isEmpty()) {
        throw new ModelNotFoundException("Model list is empty!");
    }
    return modelList;
}

Is it redundant to use throws with throw? If I remove throws ModelNotFoundException, idea underlines throw new ...

2 Answers2

0

As per your question

Is it redundant to use throws with throw?

No. It is perfectly valid.

Besides if ModelNotFoundException is an unchecked exception like NullPointerException, it is not required to declare with throws clause in the method.

if ModelNotFoundException is a checked exception like FileNotFoundException, it is similar to your code. It means you have to declare it with throws and also use throw based upon condition.

You may get answers from seniors also about it.

PythonLearner
  • 1,416
  • 7
  • 22
0

Seems like ModelNotFoundException is checked exception, therefore you should indicate that the method throws that. If you change it to unchecked exception, it will compile.

Understanding checked vs unchecked exceptions in Java

Rauf Aghayev
  • 300
  • 1
  • 12