How do I handle exceptions in Stream.forEach() method?

When using Java’s Stream.forEach() method, you might encounter checked exceptions. Checked exceptions can’t be thrown inside a lambda without being caught because of the Consumer functional interface. It does not allow for this in its method signature.

Here is an example of how you might deal with an exception in a forEach operation:

package org.kodejava.basic;

import java.util.List;

public class ForEachException {
    public static void main(String[] args) {
        List<String> list = List.of("Java", "Kotlin", "Scala", "Clojure");
        list.stream().forEach(item -> {
            try {
                // methodThatThrowsExceptions can be any method that throws a 
                // checked exception
                methodThatThrowsExceptions(item);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public static void methodThatThrowsExceptions(String item) throws Exception {
        // Method implementation
    }
}

In the above code, we have a method methodThatThrowsExceptions that can throw a checked exception. In the forEach operation, we have a lambda in which we use a try-catch block to handle potential exceptions from methodThatThrowsExceptions.

However, this approach is not generally recommended because it suppresses the exception where it occurs and doesn’t interrupt the stream processing. If you need to properly handle the exception and perhaps stop processing, you may need to use a traditional for-or-each loop.

There are several reasons why exception handling within lambda expressions in Java Streams is not generally recommended.

  1. Checked Exceptions: Lambda expressions in Java do not permit checked exceptions to be thrown, so you must handle these within the lambda expression itself. This often results in bloated, less readable lambda expressions due to the necessity of a try-catch block.

  2. Suppressed Exceptions: If you catch the exception within the lambda and print the stack trace – or worse, do nothing at all – the exception is effectively suppressed. This could lead to silent failures in your code, where an error condition is not properly propagated up the call stack. This can make it harder to debug issues, as you may be unaware an exception has occurred.

  3. Robust Error Handling: Handling the exception within the lambda expression means you’re dealing with it right at the point of failure, and it might not be the best place to handle the exception. Often, you’ll want to stop processing the current operation when an exception occurs. Propagate the error up to a higher level in your software where it can be handled properly (e.g., by displaying an error message to the user, logging the issue, or retrying the operation).

  4. Impure Functions: By handling exceptions (a side effect) within lambda expressions, we are making them impure functions – i.e., functions that modify state outside their scope or depend on state from outside their scope. This goes against the principles of functional programming.

In summary, while you can handle exceptions within forEach lambda expressions in Java, doing so can create challenges in how the software handles errors, potentially leading to suppressed exceptions, less readable code, and deviations from functional programming principles. Better approaches often are to handle exceptions at a higher level, use optional values, or use features from new versions of Java (like CompletableFuture.exceptionally) or third-party libraries designed to handle exceptions in functional programming contexts.

How do I use multi-catch statement?

The multi-catch is a language enhancement feature introduces in the Java 7. This allows us to use a single catch block to handle multiple exceptions. Each exception is separated by the pipe symbol (|).

Using the multi-catch simplify our exception handling and also reduce code duplicates in the catch block. Let’s see an example below:

package org.kodejava.lang;

import java.io.IOException;
import java.sql.SQLException;

public class MultiCatchDemo {
    public static void main(String[] args) {
        MultiCatchDemo demo = new MultiCatchDemo();
        try {
            demo.callA();
            demo.callB();
            demo.callC();
        } catch (IOException | SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void callA() throws IOException {
        throw new IOException("IOException");
    }

    private void callB() throws SQLException {
        throw new SQLException("SQLException");
    }

    private void callC() throws ClassNotFoundException {
        throw new ClassNotFoundException("ClassNotFoundException");
    }
}

How do I create custom exception class?

You can define your own exception class for your application specific purposes. The exception class is created by extending the java.lang.Exception class for checked exception or java.lang.RuntimeException for unchecked exception. By creating your own Exception classes, you could identify the problem more precisely.

package org.kodejava.basic;

public class CustomExceptionExample {
    public static void main(String[] args) {
        int x = 1, y = 0;

        try {
            int z = CustomExceptionExample.divide(x, y);
            System.out.println("z = " + z);
        } catch (DivideByZeroException e) {
            e.printStackTrace();
        }
    }

    public static int divide(int x, int y) throws DivideByZeroException {
        try {
            return (x / y);
        } catch (ArithmeticException e) {
            String m = x + " / " + y + ", trying to divide by zero";
            throw new DivideByZeroException(m, e);
        }
    }
}
package org.kodejava.basic;

class DivideByZeroException extends Exception {
    DivideByZeroException() {
        super();
    }

    DivideByZeroException(String message) {
        super(message);
    }

    DivideByZeroException(String message, Throwable cause) {
        super(message, cause);
    }
}

How do I use checked and unchecked exception?

By throwing a checked exception, you force the caller to handle the exception in a catch block. If a method throws a checked exception, it must declare that it throw the exception in the method declaration.

All exceptions are checked exceptions, except for those indicated by java.lang.Error, java.lang.RuntimeException, and their subclasses.

Runtime exception are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. Runtime exceptions are those indicated by java.lang.RuntimeException and its subclasses.

RuntimeException are known as unchecked exception. It doesn’t require to declare the unchecked exception in the method declaration.

package org.kodejava.basic;

import java.io.File;
import java.io.IOException;

public class ExceptionExample {
    public static void main(String[] args) {
        // You must catch the checked exception otherwise you get a
        // compile time error.
        try {
            ExceptionExample.checkFileSize("data.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // The unchecked exception doesn't requires you to catch
        // it and it doesn't produce a compile time error.
        ExceptionExample.divide();
    }

    /**
     * This method throws a Checked Exception, so it must declare the
     * Exception in its method declaration
     *
     * @param fileName given file name
     * @throws IOException when the file size is to large.
     */

    public static void checkFileSize(String fileName) throws IOException {
        File file = new File(fileName);
        if (file.length() > Integer.MAX_VALUE) {
            throw new IOException("File is too large.");
        }
    }

    /**
     * This method throws a RuntimeException.
     * There is no need to declare the Exception in method declaration
     *
     * @return a division result.
     * @throws ArithmeticException when arithmetic exception occurs.
     */
    public static int divide() throws ArithmeticException {
        int x = 1, y = 0;
        return x / y;
    }
}

How do I throw exceptions in Java?

The exceptions that you catch in a try-catch block must have been raised by a method that you’ve called. You can raise an exception with a statement that consists of the throw keyword, followed by an exception object. This exception object is an instance of any subclass of the Throwable class.

In the example below we have two static methods that throws exception. The first method, throwException() will throw an ArithmethicException when the divider is a zero value integer. The second method, printDate(Date date) will throw a NullPointerException if the date parameter value is null.

package org.kodejava.basic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ThrowExample {
    public static void main(String[] args) {
        try {
            ThrowExample.throwException();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ThrowExample.printDate(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void throwException() {
        int x = 6;
        int[] numbers = {3, 2, 1, 0};

        for (int y : numbers) {
            if (y == 0) {
                // Throws an ArithmeticException when about to
                // divide by zero.
                String message = String.format(
                        "x = %d; y = %d; a division by zero.",
                        x, y);
                throw new ArithmeticException(message);
            } else {
                int z = x / y;
                System.out.println("z = " + z);
            }
        }
    }

    public static void printDate(Date date) {
        if (date == null) {
            throw new NullPointerException("Date cannot be null.");
        }
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        System.out.println("Date: " + df.format(date));
    }
}

The output of our code:

z = 2
z = 3
z = 6
java.lang.ArithmeticException: x = 6; y = 0; a division by zero.
    at org.kodejava.basic.ThrowExample.throwException(ThrowExample.java:33)
    at org.kodejava.basic.ThrowExample.main(ThrowExample.java:10)
java.lang.NullPointerException: Date cannot be null.
    at org.kodejava.basic.ThrowExample.printDate(ThrowExample.java:43)
    at org.kodejava.basic.ThrowExample.main(ThrowExample.java:16)