Java consumer with exception

Interface Consumer

Type Parameters: T — the type of the input to the operation All Known Subinterfaces: Stream.Builder Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

This is a functional interface whose functional method is accept(Object) .

Method Summary

Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.

Method Details

accept

andThen

Returns a composed Consumer that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Exceptions in Java 8 Lambda Expressions

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We rely on other people’s code in our own work. Every day.

It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.

Lightrun is a new kind of debugger.

It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.

Learn more in this quick, 5-minute Lightrun tutorial:

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

Читайте также:  Найти повторяющиеся символы в строке python

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

announcement - icon

The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.

To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.

Connect your cluster and start monitoring your K8s costs right away:

We’re looking for a new Java technical editor to help review new articles for the site.

1. Overview

In Java 8, Lambda Expressions started to facilitate functional programming by providing a concise way to express behavior. However, the Functional Interfaces provided by the JDK don’t deal with exceptions very well – and the code becomes verbose and cumbersome when it comes to handling them.

In this article, we’ll explore some ways to deal with exceptions when writing lambda expressions.

2. Handling Unchecked Exceptions

First, let’s understand the problem with an example.

We have a List and we want to divide a constant, say 50 with every element of this list and print the results:

List integers = Arrays.asList(3, 9, 7, 6, 10, 20); integers.forEach(i -> System.out.println(50 / i));

This expression works but there’s one problem. If any of the elements in the list is 0, then we get an ArithmeticException: / by zero. Let’s fix that by using a traditional try-catch block such that we log any such exception and continue execution for next elements:

List integers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach(i -> < try < System.out.println(50 / i); >catch (ArithmeticException e) < System.err.println( "Arithmetic Exception occured : " + e.getMessage()); >>);

The use of try-catch solves the problem, but the conciseness of a Lambda Expression is lost and it’s no longer a small function as it’s supposed to be.

To deal with this problem, we can write a lambda wrapper for the lambda function. Let’s look at the code to see how it works:

static Consumer lambdaWrapper(Consumer consumer) < return i -> < try < consumer.accept(i); >catch (ArithmeticException e) < System.err.println( "Arithmetic Exception occured : " + e.getMessage()); >>; >
List integers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach(lambdaWrapper(i -> System.out.println(50 / i)));

At first, we wrote a wrapper method that will be responsible for handling the exception and then passed the lambda expression as a parameter to this method.

Читайте также:  Python try except get error message

The wrapper method works as expected but, you may argue that it’s basically removing the try-catch block from lambda expression and moving it to another method and it doesn’t reduce the actual number of lines of code being written.

This is true in this case where the wrapper is specific to a particular use case but we can make use of generics to improve this method and use it for a variety of other scenarios:

static Consumer consumerWrapper(Consumer consumer, Class clazz) < return i -> < try < consumer.accept(i); >catch (Exception ex) < try < E exCast = clazz.cast(ex); System.err.println( "Exception occured : " + exCast.getMessage()); >catch (ClassCastException ccEx) < throw ex; >> >; >
List integers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach( consumerWrapper( i -> System.out.println(50 / i), ArithmeticException.class));

As we can see, this iteration of our wrapper method takes two arguments, the lambda expression and the type of Exception to be caught. This lambda wrapper is capable of handling all data types, not just Integers, and catch any specific type of exception and not the superclass Exception.

Also, notice that we have changed the name of the method from lambdaWrapper to consumerWrapper. It’s because this method only handles lambda expressions for Functional Interface of type Consumer. We can write similar wrapper methods for other Functional Interfaces like Function, BiFunction, BiConsumer and so on.

3. Handling Checked Exceptions

Let’s modify the example from the previous section and instead of printing to the console, let’s write to a file.

static void writeToFile(Integer integer) throws IOException < // logic to write to file which throws IOException >

Note that the above method may throw the IOException.

List integers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach(i -> writeToFile(i));

On compilation, we get the error:

java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException

Because IOException is a checked exception, we must handle it explicitly. We have two options.

First, we may simply throw the exception outside of our method and take care of it somewhere else.

Alternatively, we can handle it inside the method that uses a lambda expression.

Let’s explore both of the options.

3.1. Throwing Checked Exception from Lambda Expressions

Let’s see what happens when we declare the IOException on the main method:

public static void main(String[] args) throws IOException < Listintegers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach(i -> writeToFile(i)); >

Still, we get the same error of unhandled IOException during the compilation.

java.lang.Error: Unresolved compilation problem: Unhandled exception type IOException

This is because lambda expressions are similar to Anonymous Inner Classes.

In our case, writeToFile method is the implementation of Consumer functional interface.

Let’s take a look at the Consumer‘s definition:

@FunctionalInterface public interface Consumer

As we can see accept method doesn’t declare any checked exception. This is why writeToFile isn’t allowed to throw the IOException.

The most straightforward way would be to use a try-catch block, wrap the checked exception into an unchecked exception and rethrow it:

List integers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach(i -> < try < writeToFile(i); >catch (IOException e) < throw new RuntimeException(e); >>); 

This gets the code to compile and run. However, this approach introduces the same issue we already discussed in the previous section – it’s verbose and cumbersome.

We can get better than that.

Let’s create a custom functional interface with a single accept method that throws an exception.

@FunctionalInterface public interface ThrowingConsumer

And now, let’s implement a wrapper method that’s able to rethrow the exception:

static Consumer throwingConsumerWrapper( ThrowingConsumer throwingConsumer) < return i -> < try < throwingConsumer.accept(i); >catch (Exception ex) < throw new RuntimeException(ex); >>; >

Finally, we’re able to simplify the way we use the writeToFile method:

List integers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach(throwingConsumerWrapper(i -> writeToFile(i)));

This is still a kind of a workaround, but the end result looks pretty clean and is definitely easier to maintain.

Both, the ThrowingConsumer and the throwingConsumerWrapper are generic and can be easily reused in different places of our application.

3.2. Handling a Checked Exception in Lambda Expression

In this final section, we’ll modify the wrapper to handle checked exceptions.

Since our ThrowingConsumer interface uses generics, we can easily handle any specific exception.

static Consumer handlingConsumerWrapper( ThrowingConsumer throwingConsumer, Class exceptionClass) < return i -> < try < throwingConsumer.accept(i); >catch (Exception ex) < try < E exCast = exceptionClass.cast(ex); System.err.println( "Exception occured : " + exCast.getMessage()); >catch (ClassCastException ccEx) < throw new RuntimeException(ex); >> >; >

Let’s see how to use it in practice:

List integers = Arrays.asList(3, 9, 7, 0, 10, 20); integers.forEach(handlingConsumerWrapper( i -> writeToFile(i), IOException.class));

Note, that the above code handles only IOException, whereas any other kind of exception is rethrown as a RuntimeException .

4. Conclusion

In this article, we showed how to handle a specific exception in lambda expression without losing the conciseness with the help of wrapper methods. We also learned how to write throwing alternatives for the Functional Interfaces present in JDK to either throw or handle a checked exception.

The complete source code of Functional Interface and wrapper methods can be downloaded from here and test classes from here, over on Github.

If you are looking for the out-of-the-box working solutions, ThrowingFunction project is worth checking out.

announcement - icon

Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.

The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.

Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.

Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:

Источник

Оцените статью