Java 8 introduced the Java Streams API to allow functional-style operations on streams of data. Streams represent a sequence of elements and support different kinds of methods like filter, map, reduce and so on. Streams can be created from various data sources like collections, arrays, files, regular expressions etc.
While streams provide a powerful and concise way to manipulate data, exception handling when using streams requires some care. We will look at how exceptions are handled in streams and best practices for handling them properly in this article.
If you don’t have a medium membership, you can use this link to reach the article without a paywall.
Exceptions in Intermediate and Terminal Operations
Operations on streams can be divided into intermediate and terminal operations.
Intermediate operations like filter, map, sorted etc. return another stream. They are lazy, meaning they don’t process the data until a terminal operation is called.