= ThrowingConsumer
:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

A _Consumer_ that allows invocation of code that throws a checked _Exception_ .

== API

[source,java]
.ThrowingConsumer.java
----
interface ThrowingConsumer<T> {
  void acceptWithException(T t)     // <.>
  void accept(T t)     // <.>
  void accept(T t, BiFunction<String, Exception, RuntimeException> exceptionWrapper)     // <.>
  ThrowingConsumer<T> throwing(BiFunction<String, Exception, RuntimeException> exceptionWrapper)     // <.>
  ThrowingConsumer<T> of(ThrowingConsumer<T> consumer)     // <.>
  ThrowingConsumer<T> of(ThrowingConsumer<T> consumer, BiFunction<String, Exception, RuntimeException> exceptionWrapper)     // <.>
}
----

<.> xref:#acceptWithException_T[acceptWithException(T)]
+
--
Performs this operation on the given argument, possibly throwing a checked exception.
--
<.> xref:#accept_T[accept(T)]
+
--
Default _Consumer#accept(Object)_ that wraps any thrown checked exceptions (by default in a _RuntimeException_ ).
--
<.> xref:#accept_T_BiFunction[accept(T, BiFunction)]
+
--
Performs this operation on the given argument, wrapping any thrown checked exceptions using the given _exceptionWrapper_ .
--
<.> xref:#throwing_BiFunction[throwing(BiFunction)]
+
--
Return a new xref:refguide:commons:index/functional/ThrowingConsumer.adoc[ThrowingConsumer] where the _#accept(Object)_ method wraps any thrown checked exceptions using the given _exceptionWrapper_ .
--
<.> xref:#of_ThrowingConsumer[of(ThrowingConsumer)]
+
--
Lambda friendly convenience method that can be used to create a xref:refguide:commons:index/functional/ThrowingConsumer.adoc[ThrowingConsumer] where the _#accept(Object)_ method wraps any checked exception thrown by the supplied lambda expression or method reference.
--
<.> xref:#of_ThrowingConsumer_BiFunction[of(ThrowingConsumer, BiFunction)]
+
--
Lambda friendly convenience method that can be used to create a xref:refguide:commons:index/functional/ThrowingConsumer.adoc[ThrowingConsumer] where the _#accept(Object)_ method wraps any thrown checked exceptions using the given _exceptionWrapper_ .
--

== Members

[#acceptWithException_T]
=== acceptWithException(T)

Performs this operation on the given argument, possibly throwing a checked exception.

[#accept_T]
=== accept(T)

Default _Consumer#accept(Object)_ that wraps any thrown checked exceptions (by default in a _RuntimeException_ ).

[#accept_T_BiFunction]
=== accept(T, BiFunction)

Performs this operation on the given argument, wrapping any thrown checked exceptions using the given _exceptionWrapper_ .

[#throwing_BiFunction]
=== throwing(BiFunction)

Return a new xref:refguide:commons:index/functional/ThrowingConsumer.adoc[ThrowingConsumer] where the _#accept(Object)_ method wraps any thrown checked exceptions using the given _exceptionWrapper_ .

[#of_ThrowingConsumer]
=== of(ThrowingConsumer)

Lambda friendly convenience method that can be used to create a xref:refguide:commons:index/functional/ThrowingConsumer.adoc[ThrowingConsumer] where the _#accept(Object)_ method wraps any checked exception thrown by the supplied lambda expression or method reference.

This method can be especially useful when working with method references. It allows you to easily convert a method that throws a checked exception into an instance compatible with a regular _Consumer_ .

For example:

----

list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException));
----

[#of_ThrowingConsumer_BiFunction]
=== of(ThrowingConsumer, BiFunction)

Lambda friendly convenience method that can be used to create a xref:refguide:commons:index/functional/ThrowingConsumer.adoc[ThrowingConsumer] where the _#accept(Object)_ method wraps any thrown checked exceptions using the given _exceptionWrapper_ .

This method can be especially useful when working with method references. It allows you to easily convert a method that throws a checked exception into an instance compatible with a regular _Consumer_ .

For example:

----

list.forEach(ThrowingConsumer.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
----
