= ThrowingSupplier
: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 _Supplier_ that allows invocation of code that throws a checked exception.

== API

[source,java]
.ThrowingSupplier.java
----
interface ThrowingSupplier<T> {
  T getWithException()     // <.>
  T get()     // <.>
  T get(BiFunction<String, Exception, RuntimeException> exceptionWrapper)     // <.>
  ThrowingSupplier<T> throwing(BiFunction<String, Exception, RuntimeException> exceptionWrapper)     // <.>
  ThrowingSupplier<T> of(ThrowingSupplier<T> supplier)     // <.>
  ThrowingSupplier<T> of(ThrowingSupplier<T> supplier, BiFunction<String, Exception, RuntimeException> exceptionWrapper)     // <.>
}
----

<.> xref:#getWithException_[getWithException()]
+
--
Gets a result, possibly throwing a checked exception.
--
<.> xref:#get_[get()]
+
--
Default _Supplier#get()_ that wraps any thrown checked exceptions (by default in a _RuntimeException_ ).
--
<.> xref:#get_BiFunction[get(BiFunction)]
+
--
Gets a result, wrapping any thrown checked exceptions using the given _exceptionWrapper_ .
--
<.> xref:#throwing_BiFunction[throwing(BiFunction)]
+
--
Return a new xref:refguide:commons:index/functional/ThrowingSupplier.adoc[ThrowingSupplier] where the _#get()_ method wraps any thrown checked exceptions using the given _exceptionWrapper_ .
--
<.> xref:#of_ThrowingSupplier[of(ThrowingSupplier)]
+
--
Lambda friendly convenience method that can be used to create a xref:refguide:commons:index/functional/ThrowingSupplier.adoc[ThrowingSupplier] where the _#get()_ method wraps any checked exception thrown by the supplied lambda expression or method reference.
--
<.> xref:#of_ThrowingSupplier_BiFunction[of(ThrowingSupplier, BiFunction)]
+
--
Lambda friendly convenience method that can be used to create xref:refguide:commons:index/functional/ThrowingSupplier.adoc[ThrowingSupplier] where the _#get()_ method wraps any thrown checked exceptions using the given _exceptionWrapper_ .
--

== Members

[#getWithException_]
=== getWithException()

Gets a result, possibly throwing a checked exception.

[#get_]
=== get()

Default _Supplier#get()_ that wraps any thrown checked exceptions (by default in a _RuntimeException_ ).

[#get_BiFunction]
=== get(BiFunction)

Gets a result, wrapping any thrown checked exceptions using the given _exceptionWrapper_ .

[#throwing_BiFunction]
=== throwing(BiFunction)

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

[#of_ThrowingSupplier]
=== of(ThrowingSupplier)

Lambda friendly convenience method that can be used to create a xref:refguide:commons:index/functional/ThrowingSupplier.adoc[ThrowingSupplier] where the _#get()_ 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 _Supplier_ .

For example:

----
optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException));
----

[#of_ThrowingSupplier_BiFunction]
=== of(ThrowingSupplier, BiFunction)

Lambda friendly convenience method that can be used to create xref:refguide:commons:index/functional/ThrowingSupplier.adoc[ThrowingSupplier] where the _#get()_ 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 _Supplier_ .

For example:

----
optional.orElseGet(ThrowingSupplier.of(Example::methodThatCanThrowCheckedException, IllegalStateException::new));
----
