Package org.djutils.exceptions
Class Try
java.lang.Object
org.djutils.exceptions.Try
public final class Try extends Object
The Try class has a number of static methods that make it easy to try-catch an exception for any Throwable class, including
the standard Java exceptions and exceptions from libraries that are used in the project. Instead of:
Try is not suitable for try-with-resource statements.
Try also has a few methods to aid JUNIT tests:
FileInputStream fis; try { fis = new FileInputStream(fileString); } catch (FileNotFoundException exception) { throw new IllegalArgumentException("File " + fileString + " is not a valid file.", exception); } try { fis.close(); } catch (IOException exception) { throw new RuntimeException("Could not close the file.", exception); }we can write:
FileInputStream fis = Try.assign(() -> new FileInputStream(fileString), IllegalArgumentException.class, "File %s is not a valid file.", fileString); Try.execute(() -> fis.close(), "Could not close the file.");The exception message can be formatted with additional arguments, such that the overhead of building the exception message only occurs if the exception condition is met. For each method there is a version without Throwable class, in which case a RuntimeException will be thrown.
Try is not suitable for try-with-resource statements.
Try also has a few methods to aid JUNIT tests:
testFail(...)
and testNotFail(...)
.
Copyright (c) 2016-2020 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
Try.Assignment<V>
Functional interface for calls to Try.assign(...).static interface
Try.Execution
Functional interface for calls to Try.execute(...). -
Method Summary
Modifier and Type Method Description static <V, T extends Throwable>
Vassign(Try.Assignment<V> assignment, Class<T> throwableClass, String message)
Tries to return a value to assign.static <V, T extends Throwable>
Vassign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg)
Tries to return a value to assign.static <V, T extends Throwable>
Vassign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2)
Tries to return a value to assign.static <V, T extends Throwable>
Vassign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3)
Tries to return a value to assign.static <V, T extends Throwable>
Vassign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3, Object... args)
Tries to return a value to assign.static <V> V
assign(Try.Assignment<V> assignment, String message)
Tries to return a value to assign.static <V> V
assign(Try.Assignment<V> assignment, String message, Object arg)
Tries to return a value to assign.static <V> V
assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2)
Tries to return a value to assign.static <V> V
assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2, Object arg3)
Tries to return a value to assign.static <V> V
assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2, Object arg3, Object... args)
Tries to return a value to assign.static <T extends Throwable>
voidexecute(Try.Execution execution, Class<T> throwableClass, String message)
Tries to execute.static <T extends Throwable>
voidexecute(Try.Execution execution, Class<T> throwableClass, String message, Object arg)
Tries to execute.static <T extends Throwable>
voidexecute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2)
Tries to execute.static <T extends Throwable>
voidexecute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3)
Tries to execute.static <T extends Throwable>
voidexecute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3, Object... args)
Tries to execute.static void
execute(Try.Execution execution, String message)
Tries to execute.static void
execute(Try.Execution execution, String message, Object arg)
Tries to execute.static void
execute(Try.Execution execution, String message, Object arg1, Object arg2)
Tries to execute.static void
execute(Try.Execution execution, String message, Object arg1, Object arg2, Object arg3)
Tries to execute.static void
execute(Try.Execution execution, String message, Object arg1, Object arg2, Object arg3, Object... args)
Tries to execute.static <V> V
testFail(Try.Assignment<V> assignment)
Fails if the assignment succeeds.static <V, T extends Throwable>
VtestFail(Try.Assignment<V> assignment, Class<T> throwableClass)
Fails if the assignment succeeds.static <V> V
testFail(Try.Assignment<V> assignment, String message)
Fails if the assignment succeeds.static <V, T extends Throwable>
VtestFail(Try.Assignment<V> assignment, String message, Class<T> throwableClass)
Fails if the assignment succeeds.static void
testFail(Try.Execution execution)
Fails if the execution succeeds.static <T extends Throwable>
voidtestFail(Try.Execution execution, Class<T> throwableClass)
Fails if the execution succeeds.static void
testFail(Try.Execution execution, String message)
Fails if the execution succeeds.static <T extends Throwable>
voidtestFail(Try.Execution execution, String message, Class<T> throwableClass)
Fails if the execution succeeds.static <V> V
testSucceed(Try.Assignment<V> assignment)
Fails if the assignment does not succeed.static <V, T extends Throwable>
VtestSucceed(Try.Assignment<V> assignment, Class<T> throwableClass)
Fails if the assignment does not succeed.static <V> V
testSucceed(Try.Assignment<V> assignment, String message)
Fails if the assignment does not succeed.static <V, T extends Throwable>
VtestSucceed(Try.Assignment<V> assignment, String message, Class<T> throwableClass)
Fails if the assignment does not succeed.static void
testSucceed(Try.Execution execution)
Fails if the execution does not succeed.static <T extends Throwable>
voidtestSucceed(Try.Execution execution, Class<T> throwableClass)
Fails if the execution does not succeed.static void
testSucceed(Try.Execution execution, String message)
Fails if the execution does not succeed.static <T extends Throwable>
voidtestSucceed(Try.Execution execution, String message, Class<T> throwableClass)
Fails if the execution does not succeed.
-
Method Details
-
assign
Tries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; the message to use in the throwable- Returns:
- V; value to assign
- Throws:
RuntimeException
- on failed Try
-
assign
public static <V> V assign(Try.Assignment<V> assignment, String message, Object arg) throws RuntimeExceptionTries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; the message to use in the throwable, with formatting identifierarg
- Object; value to use for the formatting identifier- Returns:
- V; value to assign
- Throws:
RuntimeException
- on failed Try
-
assign
public static <V> V assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2) throws RuntimeExceptionTries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiers- Returns:
- V; value to assign
- Throws:
RuntimeException
- on failed Try
-
assign
public static <V> V assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2, Object arg3) throws RuntimeExceptionTries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiers- Returns:
- V; value to assign
- Throws:
RuntimeException
- on failed Try
-
assign
public static <V> V assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2, Object arg3, Object... args) throws RuntimeExceptionTries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiersargs
- Object...; potential 4th and further values to use for the formatting identifiers- Returns:
- V; value to assign
- Throws:
RuntimeException
- on failed Try
-
assign
public static <V, T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message) throws T extends ThrowableTries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable- Returns:
- V; value to assign
- Throws:
T
- throwable on failed TryT extends Throwable
-
assign
public static <V, T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg) throws T extends ThrowableTries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifierarg
- Object; value to use for the formatting identifier- Returns:
- V; value to assign
- Throws:
T
- throwable on failed TryT extends Throwable
-
assign
public static <V, T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T extends ThrowableTries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiers- Returns:
- V; value to assign
- Throws:
T
- throwable on failed TryT extends Throwable
-
assign
public static <V, T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3) throws T extends ThrowableTries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiers- Returns:
- V; value to assign
- Throws:
T
- throwable on failed TryT extends Throwable
-
assign
public static <V, T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3, Object... args) throws T extends ThrowableTries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiersargs
- Object...; potential 4th and further values to use for the formatting identifiers- Returns:
- V; value to assign
- Throws:
T
- throwable on failed TryT extends Throwable
-
execute
Tries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- Execution; functional interface to executemessage
- String; the message to use in the throwable- Throws:
RuntimeException
- on failed Try
-
execute
public static void execute(Try.Execution execution, String message, Object arg) throws RuntimeExceptionTries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- Execution; functional interface to executemessage
- String; the message to use in the throwable, with formatting identifierarg
- Object; value to use for the formatting identifier- Throws:
RuntimeException
- on failed Try
-
execute
public static void execute(Try.Execution execution, String message, Object arg1, Object arg2) throws RuntimeExceptionTries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- Execution; functional interface to executemessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiers- Throws:
RuntimeException
- on failed Try
-
execute
public static void execute(Try.Execution execution, String message, Object arg1, Object arg2, Object arg3) throws RuntimeExceptionTries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- Execution; functional interface to executemessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiers- Throws:
RuntimeException
- on failed Try
-
execute
public static void execute(Try.Execution execution, String message, Object arg1, Object arg2, Object arg3, Object... args) throws RuntimeExceptionTries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- Execution; functional interface to executemessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiersargs
- Object...; potential 4th and further values to use for the formatting identifiers- Throws:
RuntimeException
- on failed Try
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message) throws T extends ThrowableTries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable- Throws:
T
- throwable on failed TryT extends Throwable
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg) throws T extends ThrowableTries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifierarg
- Object; value to use for the formatting identifier- Throws:
T
- throwable on failed TryT extends Throwable
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T extends ThrowableTries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiers- Throws:
T
- throwable on failed TryT extends Throwable
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3) throws T extends ThrowableTries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiers- Throws:
T
- throwable on failed TryT extends Throwable
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3, Object... args) throws T extends ThrowableTries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwable, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersarg3
- Object; 3rd value to use for the formatting identifiersargs
- Object...; potential 4th and further values to use for the formatting identifiers- Throws:
T
- throwable on failed TryT extends Throwable
-
testFail
Fails if the assignment succeeds.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign value- Returns:
- V; value to assign
-
testFail
Fails if the assignment succeeds.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail message- Returns:
- V; value to assign
-
testFail
public static <V, T extends Throwable> V testFail(Try.Assignment<V> assignment, Class<T> throwableClass)Fails if the assignment succeeds.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; throwable class to catch- Returns:
- V; value to assign
-
testFail
public static <V, T extends Throwable> V testFail(Try.Assignment<V> assignment, String message, Class<T> throwableClass)Fails if the assignment succeeds.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catch- Returns:
- V; value to assign
-
testSucceed
Fails if the assignment does not succeed.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign value- Returns:
- V; value to assign
-
testSucceed
Fails if the assignment does not succeed.- Type Parameters:
V
- value type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail message- Returns:
- V; value to assign
-
testSucceed
public static <V, T extends Throwable> V testSucceed(Try.Assignment<V> assignment, Class<T> throwableClass)Fails if the assignment does not succeed.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; throwable class to catch- Returns:
- V; value to assign
-
testSucceed
public static <V, T extends Throwable> V testSucceed(Try.Assignment<V> assignment, String message, Class<T> throwableClass)Fails if the assignment does not succeed.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catch- Returns:
- V; value to assign
-
testFail
Fails if the execution succeeds.- Parameters:
execution
- Execution; functional interface to execute
-
testFail
Fails if the execution succeeds.- Parameters:
execution
- Execution; functional interface to executemessage
- String; fail message
-
testFail
public static <T extends Throwable> void testFail(Try.Execution execution, Class<T> throwableClass)Fails if the execution succeeds.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executethrowableClass
- Class<T>; throwable class to catch
-
testFail
public static <T extends Throwable> void testFail(Try.Execution execution, String message, Class<T> throwableClass)Fails if the execution succeeds.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catch
-
testSucceed
Fails if the execution does not succeed.- Parameters:
execution
- Execution; functional interface to execute
-
testSucceed
Fails if the execution does not succeed.- Parameters:
execution
- Execution; functional interface to executemessage
- String; fail message
-
testSucceed
public static <T extends Throwable> void testSucceed(Try.Execution execution, Class<T> throwableClass)Fails if the execution does not succeed.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executethrowableClass
- Class<T>; throwable class to catch
-
testSucceed
public static <T extends Throwable> void testSucceed(Try.Execution execution, String message, Class<T> throwableClass)Fails if the execution does not succeed.- Type Parameters:
T
- throwable type- Parameters:
execution
- Execution; functional interface to executemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catch
-