Generic Min Heap Implementation in Java
Exception handling

Throwing Exception is not recommended because it's too generic.
It doesn't give a clue to the caller as to what went wrong.
It's recommended to use specific exceptions.

When implementing a collection abstract data type, it's good to take a look at a similar interface in the standard library, for example Deque:


How do they handle when an element cannot be added due to capacity restrictions? They throw IllegalArgumentException.
How do they handle when an element is requested but the collection is empty? They throw NoSuchElementException.


As you see, suitable specific exceptions already exist.
Also notice that these exceptions are unchecked.
It means that callers of these methods don't have to catch them.
And that seems an appropriate decision,
since the situations in which these exceptions can be thrown are quite unexpected, and should not happen under normal circumstances.

Redundant capacity variable

The member variable capacity is redundant.
The same information already exists in elements.length.

Make members final when possible

Since elements is never reassigned, it would be good to make it final,
so that you cannot reassign by mistake.

Overriding toString

Keep in mind that toString is not intended for "pretty-printing".

And for printing the content of the heap,
this implementation doesn't look useful to me.
With the null values removed,
the structure of the heap is not visible,
and without the structure, the ordering of the elements is meaningless,
which can be misleading.

For printing the content of the heap I would suggest adding a dedicated method,
keep the nulls, and print values of the first size elements.