Package org.djutils.exceptions
Class Try
java.lang.Object
org.djutils.exceptions.Try
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-2025 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 ClassesModifier and TypeClassDescriptionstatic interface
Functional interface for calls to Try.assign(...).static interface
Functional interface for calls to Try.execute(...). -
Method Summary
Modifier and TypeMethodDescriptionstatic <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
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>
voidTries 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
Tries to execute.static void
execute
(Try.Execution execution, String message, Object arg1, Object arg2, Object arg3, Object... args) Tries to execute.
-
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
- functional interface to assign valuemessage
- the message to use in the throwable- Returns:
- value to assign
- Throws:
RuntimeException
- on failed Try
-
assign
public static <V> V assign(Try.Assignment<V> assignment, String message, Object arg) throws RuntimeException Tries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- functional interface to assign valuemessage
- the message to use in the throwable, with formatting identifierarg
- value to use for the formatting identifier- Returns:
- 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 RuntimeException Tries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- functional interface to assign valuemessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiers- Returns:
- 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 RuntimeException Tries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- functional interface to assign valuemessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 3rd value to use for the formatting identifiers- Returns:
- 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 RuntimeException Tries to return a value to assign. Will throw a RuntimeException if the try fails.- Type Parameters:
V
- value type- Parameters:
assignment
- functional interface to assign valuemessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 3rd value to use for the formatting identifiersargs
- potential 4th and further values to use for the formatting identifiers- Returns:
- 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 Tries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- functional interface to assign valuethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable- Returns:
- value to assign
- Throws:
T
- throwable on failed Try
-
assign
public static <V,T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg) throws T Tries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- functional interface to assign valuethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifierarg
- value to use for the formatting identifier- Returns:
- value to assign
- Throws:
T
- throwable on failed Try
-
assign
public static <V,T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T Tries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- functional interface to assign valuethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiers- Returns:
- value to assign
- Throws:
T
- throwable on failed Try
-
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 Tries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- functional interface to assign valuethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 3rd value to use for the formatting identifiers- Returns:
- value to assign
- Throws:
T
- throwable on failed Try
-
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 Tries to return a value to assign. Will throw a specified Throwable if the try fails.- Type Parameters:
V
- value typeT
- throwable type- Parameters:
assignment
- functional interface to assign valuethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 3rd value to use for the formatting identifiersargs
- potential 4th and further values to use for the formatting identifiers- Returns:
- value to assign
- Throws:
T
- throwable on failed Try
-
execute
Tries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- functional interface to executemessage
- 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 RuntimeException Tries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- functional interface to executemessage
- the message to use in the throwable, with formatting identifierarg
- 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 RuntimeException Tries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- functional interface to executemessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 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 RuntimeException Tries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- functional interface to executemessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 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 RuntimeException Tries to execute. Will throw a RuntimeException if the try fails.- Parameters:
execution
- functional interface to executemessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 3rd value to use for the formatting identifiersargs
- 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 Tries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- functional interface to executethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable- Throws:
T
- throwable on failed Try
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg) throws T Tries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- functional interface to executethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifierarg
- value to use for the formatting identifier- Throws:
T
- throwable on failed Try
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T Tries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- functional interface to executethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiers- Throws:
T
- throwable on failed Try
-
execute
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3) throws T Tries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- functional interface to executethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 3rd value to use for the formatting identifiers- Throws:
T
- throwable on failed Try
-
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 Tries to execute. Will throw a specified Throwable if the try fails.- Type Parameters:
T
- throwable type- Parameters:
execution
- functional interface to executethrowableClass
- class of the throwable to throwmessage
- the message to use in the throwable, with formatting identifiersarg1
- 1st value to use for the formatting identifiersarg2
- 2nd value to use for the formatting identifiersarg3
- 3rd value to use for the formatting identifiersargs
- potential 4th and further values to use for the formatting identifiers- Throws:
T
- throwable on failed Try
-