Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsWhy does the 'orElseThrow()' method take a Supplier as a parameter instead of an Exception?
What is the benefit of the orElseThrow() method to expect a Supplier instead of an Exception?
Why is it this:
public CourseIdea findBySlug(String slug) {
return ideas.stream().filter(idea -> idea.getSlug().equals(slug))
.findFirst()
.orElseThrow(NotFoundException::new);
}
Instead of this:
public CourseIdea findBySlug(String slug) {
return ideas.stream().filter(idea -> idea.getSlug().equals(slug))
.findFirst()
.orElseThrow(new NotFoundException());
}
Kind Regards, Florian
1 Answer

Craig Dennis
Treehouse TeacherHi Florian Tönjes , great question!
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.
We're planning on diving deeper into functional programming very soon, and that will help explore awesome questions like these.
That make sense?
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsFlorian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 PointsSo "NotFoundException::new" is really just a method reference to the constructor, whereas "new NotFoundException()" is actually creating an object?