Why does the orElseThrow()
method in Java take a Supplier
as a parameter instead of an Exception
? This is the question and the best answer I have found is in here, stating
The overarching concept here is that by providing a method reference (or a function) it can be lazily initialized. No need to create Exception object if you don't need it. Create it only when you need it.
However, I still do not understand it. Why can't the object creation be lazy?