public final class Try extends Object
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.
testFail(...)
and testNotFail(...)
.
Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
Modifier and Type | Class and Description |
---|---|
static interface |
Try.Assignment<V>
Functional interface for calls to Try.assign(...).
|
static interface |
Try.Execution
Functional interface for calls to Try.execute(...).
|
Modifier and Type | Method and Description |
---|---|
static <V,T extends Throwable> |
assign(Try.Assignment<V> assignment,
Class<T> throwableClass,
String message)
Tries to return a value to assign.
|
static <V,T extends Throwable> |
assign(Try.Assignment<V> assignment,
Class<T> throwableClass,
String message,
Object arg)
Tries to return a value to assign.
|
static <V,T extends Throwable> |
assign(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> |
assign(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> |
assign(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> |
execute(Try.Execution execution,
Class<T> throwableClass,
String message)
Tries to execute.
|
static <T extends Throwable> |
execute(Try.Execution execution,
Class<T> throwableClass,
String message,
Object arg)
Tries to execute.
|
static <T extends Throwable> |
execute(Try.Execution execution,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2)
Tries to execute.
|
static <T extends Throwable> |
execute(Try.Execution execution,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2,
Object arg3)
Tries to execute.
|
static <T extends Throwable> |
execute(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)
Executes a JUNIT fail if the assignment succeeds.
|
static <V,T extends Throwable> |
testFail(Try.Assignment<V> assignment,
Class<T> throwableClass)
Executes a JUNIT fail if the assignment succeeds.
|
static <V> V |
testFail(Try.Assignment<V> assignment,
String message)
Executes a JUNIT fail if the assignment succeeds.
|
static <V,T extends Throwable> |
testFail(Try.Assignment<V> assignment,
String message,
Class<T> throwableClass)
Executes a JUNIT fail if the assignment succeeds.
|
static void |
testFail(Try.Execution execution)
Executes a JUNIT fail if the execution succeeds.
|
static <T extends Throwable> |
testFail(Try.Execution execution,
Class<T> throwableClass)
Executes a JUNIT fail if the execution succeeds.
|
static void |
testFail(Try.Execution execution,
String message)
Executes a JUNIT fail if the execution succeeds.
|
static <T extends Throwable> |
testFail(Try.Execution execution,
String message,
Class<T> throwableClass)
Executes a JUNIT fail if the execution succeeds.
|
static <V> V |
testSucceed(Try.Assignment<V> assignment)
Executes a JUNIT fail if the assignment does not succeed.
|
static <V,T extends Throwable> |
testSucceed(Try.Assignment<V> assignment,
Class<T> throwableClass)
Executes a JUNIT fail if the assignment does not succeed.
|
static <V> V |
testSucceed(Try.Assignment<V> assignment,
String message)
Executes a JUNIT fail if the assignment does not succeed.
|
static <V,T extends Throwable> |
testSucceed(Try.Assignment<V> assignment,
String message,
Class<T> throwableClass)
Executes a JUNIT fail if the assignment does not succeed.
|
static void |
testSucceed(Try.Execution execution)
Executes a JUNIT fail if the execution does not succeed.
|
static <T extends Throwable> |
testSucceed(Try.Execution execution,
Class<T> throwableClass)
Executes a JUNIT fail if the execution does not succeed.
|
static void |
testSucceed(Try.Execution execution,
String message)
Executes a JUNIT fail if the execution does not succeed.
|
static <T extends Throwable> |
testSucceed(Try.Execution execution,
String message,
Class<T> throwableClass)
Executes a JUNIT fail if the execution does not succeed.
|
public static <V> V assign(Try.Assignment<V> assignment, String message) throws RuntimeException
V
- value typeassignment
- Assignment<V>; functional interface to assign valuemessage
- String; the message to use in the throwableRuntimeException
- on failed Trypublic static <V> V assign(Try.Assignment<V> assignment, String message, Object arg) throws RuntimeException
V
- value typeassignment
- 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 identifierRuntimeException
- on failed Trypublic static <V> V assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2) throws RuntimeException
V
- value typeassignment
- 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 identifiersRuntimeException
- on failed Trypublic static <V> V assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2, Object arg3) throws RuntimeException
V
- value typeassignment
- 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 identifiersRuntimeException
- on failed Trypublic static <V> V assign(Try.Assignment<V> assignment, String message, Object arg1, Object arg2, Object arg3, Object... args) throws RuntimeException
V
- value typeassignment
- 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 identifiersRuntimeException
- on failed Trypublic static <V,T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message) throws T extends Throwable
V
- value typeT
- throwable typeassignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwableT
- throwable on failed TryT extends Throwable
public static <V,T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg) throws T extends Throwable
V
- value typeT
- throwable typeassignment
- 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 identifierT
- throwable on failed TryT extends Throwable
public static <V,T extends Throwable> V assign(Try.Assignment<V> assignment, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T extends Throwable
V
- value typeT
- throwable typeassignment
- 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 identifiersT
- throwable on failed TryT extends Throwable
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 Throwable
V
- value typeT
- throwable typeassignment
- 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 identifiersT
- throwable on failed TryT extends Throwable
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 Throwable
V
- value typeT
- throwable typeassignment
- 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 identifiersT
- throwable on failed TryT extends Throwable
public static void execute(Try.Execution execution, String message) throws RuntimeException
execution
- Execution; functional interface to executemessage
- String; the message to use in the throwableRuntimeException
- on failed Trypublic static void execute(Try.Execution execution, String message, Object arg) throws RuntimeException
execution
- Execution; functional interface to executemessage
- String; the message to use in the throwable, with formatting identifierarg
- Object; value to use for the formatting identifierRuntimeException
- on failed Trypublic static void execute(Try.Execution execution, String message, Object arg1, Object arg2) throws RuntimeException
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 identifiersRuntimeException
- on failed Trypublic static void execute(Try.Execution execution, String message, Object arg1, Object arg2, Object arg3) throws RuntimeException
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 identifiersRuntimeException
- on failed Trypublic static void execute(Try.Execution execution, String message, Object arg1, Object arg2, Object arg3, Object... args) throws RuntimeException
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 identifiersRuntimeException
- on failed Trypublic static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message) throws T extends Throwable
T
- throwable typeexecution
- Execution; functional interface to executethrowableClass
- Class<T>; class of the throwable to throwmessage
- String; the message to use in the throwableT
- throwable on failed TryT extends Throwable
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg) throws T extends Throwable
T
- throwable typeexecution
- 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 identifierT
- throwable on failed TryT extends Throwable
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T extends Throwable
T
- throwable typeexecution
- 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 identifiersT
- throwable on failed TryT extends Throwable
public static <T extends Throwable> void execute(Try.Execution execution, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3) throws T extends Throwable
T
- throwable typeexecution
- 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 identifiersT
- throwable on failed TryT extends Throwable
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 Throwable
T
- throwable typeexecution
- 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 identifiersT
- throwable on failed TryT extends Throwable
public static <V> V testFail(Try.Assignment<V> assignment)
V
- value typeassignment
- Assignment<V>; functional interface to assign valuepublic static <V> V testFail(Try.Assignment<V> assignment, String message)
V
- value typeassignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail messagepublic static <V,T extends Throwable> V testFail(Try.Assignment<V> assignment, Class<T> throwableClass)
V
- value typeT
- throwable typeassignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; throwable class to catchpublic static <V,T extends Throwable> V testFail(Try.Assignment<V> assignment, String message, Class<T> throwableClass)
V
- value typeT
- throwable typeassignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catchpublic static <V> V testSucceed(Try.Assignment<V> assignment)
V
- value typeassignment
- Assignment<V>; functional interface to assign valuepublic static <V> V testSucceed(Try.Assignment<V> assignment, String message)
V
- value typeassignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail messagepublic static <V,T extends Throwable> V testSucceed(Try.Assignment<V> assignment, Class<T> throwableClass)
V
- value typeT
- throwable typeassignment
- Assignment<V>; functional interface to assign valuethrowableClass
- Class<T>; throwable class to catchpublic static <V,T extends Throwable> V testSucceed(Try.Assignment<V> assignment, String message, Class<T> throwableClass)
V
- value typeT
- throwable typeassignment
- Assignment<V>; functional interface to assign valuemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catchpublic static void testFail(Try.Execution execution)
execution
- Execution; functional interface to executepublic static void testFail(Try.Execution execution, String message)
execution
- Execution; functional interface to executemessage
- String; fail messagepublic static <T extends Throwable> void testFail(Try.Execution execution, Class<T> throwableClass)
T
- throwable typeexecution
- Execution; functional interface to executethrowableClass
- Class<T>; throwable class to catchpublic static <T extends Throwable> void testFail(Try.Execution execution, String message, Class<T> throwableClass)
T
- throwable typeexecution
- Execution; functional interface to executemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catchpublic static void testSucceed(Try.Execution execution)
execution
- Execution; functional interface to executepublic static void testSucceed(Try.Execution execution, String message)
execution
- Execution; functional interface to executemessage
- String; fail messagepublic static <T extends Throwable> void testSucceed(Try.Execution execution, Class<T> throwableClass)
T
- throwable typeexecution
- Execution; functional interface to executethrowableClass
- Class<T>; throwable class to catchpublic static <T extends Throwable> void testSucceed(Try.Execution execution, String message, Class<T> throwableClass)
T
- throwable typeexecution
- Execution; functional interface to executemessage
- String; fail messagethrowableClass
- Class<T>; throwable class to catchCopyright © 2014–2018 Delft University of Technology. All rights reserved.