public final class Throw extends Object
if (car == null) { throw new NullPointerException("Car may not be null."); } if (Double.isNaN(car.getPosition())) { throw new IllegalArgumentException("Position of car " + car + " is NaN."); }we can write:
Throw.whenNull(car, "Car may not be null."); Throw.when(Double.isNaN(car.getPosition()), IllegalArgumentException.class, "Position of car %s is NaN.", car);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. All methods have a version where the first parameter is returned. Thereby, the Throw can be used as part of a super(...) call in a constructor.
Copyright (c) 2016-2019 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.
Modifier and Type | Method and Description |
---|---|
static <T extends Throwable> |
when(boolean condition,
Class<T> throwableClass,
String message)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable> |
when(boolean condition,
Class<T> throwableClass,
String message,
Object arg)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable> |
when(boolean condition,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable> |
when(boolean condition,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2,
Object arg3)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable> |
when(boolean condition,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2,
Object arg3,
Object... args)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable,O> |
when(O object,
boolean condition,
Class<T> throwableClass,
String message)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable,O> |
when(O object,
boolean condition,
Class<T> throwableClass,
String message,
Object arg)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable,O> |
when(O object,
boolean condition,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable,O> |
when(O object,
boolean condition,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2,
Object arg3)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <T extends Throwable,O> |
when(O object,
boolean condition,
Class<T> throwableClass,
String message,
Object arg1,
Object arg2,
Object arg3,
Object... args)
Throw a Throwable (such as an Exception or Error) if a condition is met, e.g.
|
static <O> O |
whenNull(O object,
String message)
Throw a NullPointerException if object is null, e.g.
|
static <O> O |
whenNull(O object,
String message,
Object arg)
Throw a NullPointerException if object is null, e.g.
|
static <O> O |
whenNull(O object,
String message,
Object arg1,
Object arg2)
Throw a NullPointerException if object is null, e.g.
|
static <O> O |
whenNull(O object,
String message,
Object arg1,
Object arg2,
Object arg3)
Throw a NullPointerException if object is null, e.g.
|
static <O> O |
whenNull(O object,
String message,
Object arg1,
Object arg2,
Object arg3,
Object... args)
Throw a NullPointerException if object is null, e.g.
|
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message) throws T extends Throwable
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN.");
T
- the Throwable typecondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exceptionT
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg) throws T extends Throwable
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s.", object);
T
- the Throwable typecondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, with formatting identifiersarg
- Object; value to use for the formatting identifiersT
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T extends Throwable
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s.", object, name);
T
- the Throwable typecondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersT
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3) throws T extends Throwable
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s and id %s.", object, name, id);
T
- the Throwable typecondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, 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
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3, Object... args) throws T extends Throwable
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s, id %s and parent %s.", object, name, id, parent);
T
- the Throwable typecondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, 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
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message) throws T extends Throwable
super(Throw.when(object, Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN."));
T
- the Throwable typeO
- the Object type to returnobject
- O; the object to return by this static methodcondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exceptionT
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message, Object arg) throws T extends Throwable
super(Throw.when(object, Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s.", object));
T
- the Throwable typeO
- the Object type to returnobject
- O; the object to return by this static methodcondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, with formatting identifiersarg
- Object; value to use for the formatting identifiersT
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T extends Throwable
super(Throw.when(object, Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s.", object, name));
T
- the Throwable typeO
- the Object type to returnobject
- O; the object to return by this static methodcondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersT
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3) throws T extends Throwable
super(Throw.when(object, Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s and id %s.", object, name, id));
T
- the Throwable typeO
- the Object type to returnobject
- O; the object to return by this static methodcondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, 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
- the throwable to throw on true conditionT extends Throwable
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3, Object... args) throws T extends Throwable
super(Throw.when(object, Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s, id %s and parent %s.", object, name, id, parent));
T
- the Throwable typeO
- the Object type to returnobject
- O; the object to return by this static methodcondition
- the condition to check; an exception will be thrown if this is truethrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, 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
- the throwable to throw on true conditionT extends Throwable
public static <O> O whenNull(O object, String message) throws NullPointerException
Throw.when(object.getValue(), "Value may not be null.");
O
- the Object type to returnobject
- object to check; an exception will be thrown if this is nullmessage
- String; the message to use in the exceptionNullPointerException
- if object is nullpublic static <O> O whenNull(O object, String message, Object arg) throws NullPointerException
Throw.whenNull(object.getValue(), "Value may not be null for object %s.", object);
O
- the Object type to returnobject
- object to check; an exception will be thrown if this is nullmessage
- String; the message to use in the exception, with formatting identifiersarg
- Object; value to use for the formatting identifiersNullPointerException
- if object is nullpublic static <O> O whenNull(O object, String message, Object arg1, Object arg2) throws NullPointerException
Throw.whenNull(object.getValue(), "Value may not be null for object %s with name %s.", object, name);
O
- the Object type to returnobject
- object to check; an exception will be thrown if this is nullmessage
- String; the message to use in the exception, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersarg2
- Object; 2nd value to use for the formatting identifiersNullPointerException
- if object is nullpublic static <O> O whenNull(O object, String message, Object arg1, Object arg2, Object arg3) throws NullPointerException
Throw.whenNull(object.getValue(), "Value may not be null for object %s with name %s and id %s.", object, name, id);
O
- the Object type to returnobject
- object to check; an exception will be thrown if this is nullmessage
- String; the message to use in the exception, 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 identifiersNullPointerException
- if object is nullpublic static <O> O whenNull(O object, String message, Object arg1, Object arg2, Object arg3, Object... args) throws NullPointerException
Throw.whenNull(object.getValue(), "Value may not be null for object %s with name %s, id %s and parent %s.", object, name, id, parent);
O
- the Object type to returnobject
- object to check; an exception will be thrown if this is nullmessage
- String; the message to use in the exception, 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 identifiersNullPointerException
- if object is nullCopyright © 2018–2019 Delft University of Technology. All rights reserved.