Try objects are constructed using the catching(IThrowingRunnable) or catching(IThrowingSupplier) factories
from code blocks which may or may not throw checked or unchecked (run-time) exceptions.
The exception is dealt with inside the Try instance.
The class provides methods to access the result, access the exception,
use an alternative result if an exception occurred, transform the result to another result amongst other methods.
Try instances are immutable.
Usage example:
// In this sample we query the last modification timestamp of a path p.
// The API throws IOException (if an I/O error occurs).
Path p = Path.of("a_path");
// Code using a conventional try/catch construct looks like this:
String str1 = null;
try {
str1 = Files.getLastModifiedTime(p).toString();
} catch (IOException ex) {
str1 = "unknown";
}
System.out.println("1: File time is: " + str1);
// The same using Try:
Try<FileTime, IOException> tc1 = Try.catching(() -> Files.getLastModifiedTime(p));
// Perform a transformation of the internal value. Here FileTime is mapped to its string representation.
Try<String, Throwable> tc2 = tc1.map(FileTime::toString);
String str2 = tc2.orElse("unknown");
System.out.println("2: File time is: " + str2);
// The same on a single line:
System.out.println("3: File time is: " + Try.catching(() -> Files.getLastModifiedTime(p).toString()).orElse("unknown"));
// Similarly the class supports try-with-resources scenarios such as:
List<File> files = Try.withResources(() -> Files.walk(p), w -> {
return w.filter(Files::isRegularFile)
.map(Path::toFile)
.collect(Collectors.toList());
}).orIgnore();
| Modifier and Type | Field and Description |
|---|---|
| private final EC | t
The immutable exception or |
| private final V | val
The immutable value or |
| Access | Constructor and Description |
|---|---|
| private | Try(IThrowingSupplier<V, EC>
a throwing supplier catchable)Creates a new instance from a throwing supplier. |
| private | Try(V
value, may be null val, EC exception, may be null ex)Creates a new instance from a value and an exception. |
| Modifier and Type | Method and Description |
|---|---|
| public static < the type of value supplied by the supplier R, the type of exception, if any, thrown by the supplier EC extends Throwable> Try | Returns: new instancea throwing supplier catchable)Creates a new instance from a throwing |
| public static < the type of exception, if any, thrown by the runnable EC extends Throwable> Try | Returns: new instancea throwing runnable catchable)Creates a new instance from a throwing |
| private static < the type of Throwable T extends Throwable> RuntimeException | |
| public V | |
| public EC | |
| pack-priv boolean | Returns: true if an exception has occurred, false otherwise.Indicates whether an exception has occurred. |
| public < the type result produced by the mapper V1, the type of exception thrown by the mapper E1 extends Throwable> Try | |
| public V | Returns: the successful value, otherwise theother valuethe value to be returned, if an exception has occurred. May be other)null.Returns the successful value or if an exception has occurred returns |
| public void | orElse(IThrowingConsumer<Throwable, Throwable>
consumer to execute in case of exception. The exception object is passed to the consumer. consumer)Executes |
| public V | Returns: the successful value or value returned by the functionfunction to execute in case of exception. The exception object is passed to the function. function)If an exception has occurred, executes |
| public V | Returns: the successful value or value returned by the suppliersupplier that supplies an alternative value supplier)If an exception has occurred, executes |
| public V | Returns: the successful value or nullReturns the value on success or |
| public V | Returns: the successful valueReturns the successful value or throws the exception that occurred earlier. |
| public < the type of exception returned by T2 extends Throwable> Vfunction | |
| public String | |
| public static < the type of resource (must implement R extends AutoCloseable, AutoCloseable)the type of exception, if any, thrown by the resource supplier ES extends Throwable, the type of value returned by the function V, the type of exception, if any, thrown by the function EC extends Throwable> Try | Returns: new instancethe resource supplier resourceSupplier, IThrowingFunction<R, V, EC> the function executed on the resource catchable)Try-with-resources support for a single resource. |
| public static <R extends AutoCloseable, ES extends Throwable, EC extends Throwable> Try | withResources(IThrowingSupplier<R, ES> resourceSupplier, IThrowingConsumer<R, EC> catchable)
Try-with-resources support for a single resource with a |
| t | back to summary |
|---|---|
| private final EC t The immutable exception or | |
| val | back to summary |
|---|---|
| private final V val The immutable value or | |
| Try | back to summary |
|---|---|
| private Try(IThrowingSupplier<V, EC> catchable) Creates a new instance from a throwing supplier.
| |
| Try | back to summary |
|---|---|
| private Try(V val, EC ex) Creates a new instance from a value and an exception.
| |
| catching | back to summary |
|---|---|
| public static <R, EC extends Throwable> Try Creates a new instance from a throwing
| |
| catching | back to summary |
|---|---|
| public static <EC extends Throwable> Try Creates a new instance from a throwing
| |
| doThrow | back to summary |
|---|---|
| private static <T extends Throwable> RuntimeException doThrow(Throwable t) throws T Performs a vacuous cast on a Throwable to evade the Java checked exception mechanism.
| |
| get | back to summary |
|---|---|
| public V get() Gets the value or re-throws the exception if one has occurred.
| |
| getException | back to summary |
|---|---|
| public EC getException() Gets the exception or
| |
| hasThrown | back to summary |
|---|---|
| pack-priv boolean hasThrown() Indicates whether an exception has occurred.
| |
| map | back to summary |
|---|---|
| public <V1, E1 extends Throwable> Try Performs a transformation on the internal value, if the source object does not have an exception. | |
| orElse | back to summary |
|---|---|
| public V orElse(V other) Returns the successful value or if an exception has occurred returns
| |
| orElse | back to summary |
|---|---|
| public void orElse(IThrowingConsumer<Throwable, Throwable> consumer) Executes
| |
| orElseApply | back to summary |
|---|---|
| public V orElseApply(IThrowingFunction<Throwable, V, Throwable> function) If an exception has occurred, executes
| |
| orElseGet | back to summary |
|---|---|
| public V orElseGet(IThrowingSupplier<V, Throwable> supplier) If an exception has occurred, executes
| |
| orIgnore | back to summary |
|---|---|
| public V orIgnore() Returns the value on success or
| |
| orThrow | back to summary |
|---|---|
| public V orThrow() Returns the successful value or throws the exception that occurred earlier.
| |
| orThrow | back to summary |
|---|---|
| public <T2 extends Throwable> V orThrow(Function<Throwable, T2> function) throws Function-:T2 Returns the successful value or throws the exception returned by
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |
| withResources | back to summary |
|---|---|
| public static <R extends AutoCloseable, ES extends Throwable, V, EC extends Throwable> Try Try-with-resources support for a single resource.
| |
| withResources | back to summary |
|---|---|
| public static <R extends AutoCloseable, ES extends Throwable, EC extends Throwable> Try Try-with-resources support for a single resource with a | |