I have written this simple code to check weather a binary heap is empty or not. I have problem with return. It can not be: null
, void
, or nothing; It should return something int, but I don't know what. So, what should I put there, if I want to keep this code simple? (I mean not using Integer
class or java.lang.Integer
).
public int getMinimum() {
if (isEmpty()) {
System.out.println("Heap is empty");
return;
} else
return data[0];
}
throw a exception for invalid value and handle it when calling
public int getMinimum() throws Exception {
if (isEmpty())
throw new Exception("Heap is Empty");
else
return data[0];
}
and when getting minimum
try{
int i = getMinimum();
System.out.println("Minimum is :" + i);
}catch(Exception e){
System.out.println(e.getMessage());
}