Package org.djutils.exceptions
Class Throw
java.lang.Object
org.djutils.exceptions.Throw
The Throw class has a number of static methods that make it easy to throw an exception under conditions for any Exception
class, including the standard Java exceptions and exceptions from libraries that are used in the project. Instead of:
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-2024 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
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Throw an unchecked exception for a method with a fixed signature (e.g., extending a method from a library that cannot be changed), without having to declare the exception, which can be impossible when extending a method.static <T extends Throwable>
voidThrow a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking.static <T extends Throwable>
voidThrow a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking.static <T extends Throwable>
voidThrow a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking.static <T extends Throwable>
voidwhen
(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. for pre- and postcondition checking.static <T extends Throwable>
voidwhen
(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. for pre- and postcondition checking.static <T extends Throwable,
O>
OThrow a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking.static <T extends Throwable,
O>
OThrow a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking.static <T extends Throwable,
O>
Owhen
(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. for pre- and postcondition checking.static <T extends Throwable,
O>
Owhen
(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. for pre- and postcondition checking.static <T extends Throwable,
O>
Owhen
(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. for pre- and postcondition checking.static <T extends Throwable>
doubleThrow a specified exception if value is NaN, e.g. for pre- and postcondition checking.static double
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking.static <T extends Throwable>
floatThrow a specified exception if value is NaN, e.g. for pre- and postcondition checking.static float
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking.Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking.Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking.static Double
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking.static Double
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking.Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking.Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking.static Float
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking.static Float
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking.static <O> O
Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking.static <O> O
Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking.static <O> O
Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking.static <O> O
Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking.static <O> O
Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking.
-
Method Details
-
when
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. Use as follows:
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN.");
- Type Parameters:
T
- the Throwable type- Parameters:
condition
- boolean; 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- Throws:
T
- the throwable to throw on true condition
-
when
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. Use as follows:
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s.", object);
- Type Parameters:
T
- the Throwable type- Parameters:
condition
- boolean; 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 identifiers- Throws:
T
- the throwable to throw on true condition
-
when
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. Use as follows:
Throw.when(Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s.", object, name);
- Type Parameters:
T
- the Throwable type- Parameters:
condition
- boolean; 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 identifiers- Throws:
T
- the throwable to throw on true condition
-
when
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. Use as follows:
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);
- Type Parameters:
T
- the Throwable type- Parameters:
condition
- boolean; 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 identifiers- Throws:
T
- the throwable to throw on true condition
-
when
public static <T extends Throwable> void when(boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2, Object arg3, Object... args) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. Use as follows:
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);
- Type Parameters:
T
- the Throwable type- Parameters:
condition
- boolean; 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 identifiers- Throws:
T
- the throwable to throw on true condition
-
when
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. This version of the method returns its first parameter, so it can be used inside a constructor. Use e.g., as follows:super(Throw.when(object, Double.isNaN(object.getValue()), IllegalArgumentException.class, "Value may not be NaN."));
- Type Parameters:
T
- the Throwable typeO
- the Object type to return- Parameters:
object
- O; the object to return by this static methodcondition
- boolean; 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- Returns:
- the object that was passed as the first parameter
- Throws:
T
- the throwable to throw on true condition
-
when
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message, Object arg) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. This version of the method returns its first parameter, so it can be used inside a constructor. Use e.g., as follows:super(Throw.when(object, Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s.", object));
- Type Parameters:
T
- the Throwable typeO
- the Object type to return- Parameters:
object
- O; the object to return by this static methodcondition
- boolean; 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
T
- the throwable to throw on true condition
-
when
public static <T extends Throwable,O> O when(O object, boolean condition, Class<T> throwableClass, String message, Object arg1, Object arg2) throws T Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. This version of the method returns its first parameter, so it can be used inside a constructor. Use e.g., as follows:super(Throw.when(object, Double.isNan(object.getValue()), IllegalArgumentException.class, "Value may not be NaN for object %s with name %s.", object, name));
- Type Parameters:
T
- the Throwable typeO
- the Object type to return- Parameters:
object
- O; the object to return by this static methodcondition
- boolean; 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
T
- the throwable to throw on true condition
-
when
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 Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. This version of the method returns its first parameter, so it can be used inside a constructor. Use e.g., as follows: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));
- Type Parameters:
T
- the Throwable typeO
- the Object type to return- Parameters:
object
- O; the object to return by this static methodcondition
- boolean; 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
T
- the throwable to throw on true condition
-
when
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 Throw a Throwable (such as an Exception or Error) if a condition is met, e.g. for pre- and postcondition checking. This version of the method returns its first parameter, so it can be used inside a constructor. Use e.g., as follows: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));
- Type Parameters:
T
- the Throwable typeO
- the Object type to return- Parameters:
object
- O; the object to return by this static methodcondition
- boolean; 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
T
- the throwable to throw on true condition
-
whenNull
Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNull(value, "value may not be null.");
A shortened version where the text " may not be null" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNull(value, "value");
- Type Parameters:
O
- the Object type to return- Parameters:
object
- object to check; an exception will be thrown if the object is nullmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be null"- Returns:
- the object that was passed as the first parameter
- Throws:
NullPointerException
- if object is null
-
whenNull
Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNull(object.getValue(), "Value may not be null for object %s.", object);
- Type Parameters:
O
- the Object type to return- Parameters:
object
- 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
NullPointerException
- if object is null
-
whenNull
public static <O> O whenNull(O object, String message, Object arg1, Object arg2) throws NullPointerException Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNull(object.getValue(), "Value may not be null for object %s with name %s.", object, name);
- Type Parameters:
O
- the Object type to return- Parameters:
object
- 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
NullPointerException
- if object is null
-
whenNull
public static <O> O whenNull(O object, String message, Object arg1, Object arg2, Object arg3) throws NullPointerException Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNull(object.getValue(), "Value may not be null for object %s with name %s and id %s.", object, name, id);
- Type Parameters:
O
- the Object type to return- Parameters:
object
- 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
NullPointerException
- if object is null
-
whenNull
public static <O> O whenNull(O object, String message, Object arg1, Object arg2, Object arg3, Object... args) throws NullPointerException Throw a NullPointerException if object is null, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNull(object.getValue(), "Value may not be null for object %s with name %s, id %s and parent %s.", object, name, id, parent);
- Type Parameters:
O
- the Object type to return- Parameters:
object
- 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 identifiers- Returns:
- the object that was passed as the first parameter
- Throws:
NullPointerException
- if object is null
-
whenNaN
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, "value");
- Parameters:
value
- double; value to check; an exception will be thrown if the object is NaNmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- double; the value that was passed as the first parameter
- Throws:
ArithmeticException
- if value is NaN
-
whenNaN
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, "value");
- Parameters:
value
- float; value to check; an exception will be thrown if the object is NaNmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- float; the value that was passed as the first parameter
- Throws:
ArithmeticException
- if value is NaN
-
whenNaN
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, "value");
- Parameters:
value
- Double; value to check; an exception will be thrown if the object is NaNmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- Double; the value that was passed as the first parameter
- Throws:
ArithmeticException
- if value is NaN
-
whenNaN
public static Double whenNaN(Double value, String message, Object arg1, Object... args) throws ArithmeticException Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, "Value may not be NaN for object %s with name %s, id %s.", object, name, id);
- Parameters:
value
- Double; the value to check; an exception will be thrown if this is NaNmessage
- String; the message to use in the exception, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersargs
- Object...; potential 2nd and further values to use for the formatting identifiers- Returns:
- Double; the value that was passed as the first parameter
- Throws:
ArithmeticException
- if value is NaN
-
whenNaN
Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, "value");
- Parameters:
value
- Float; value to check; an exception will be thrown if the object is NaNmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- Float; the value that was passed as the first parameter
- Throws:
ArithmeticException
- if value is NaN
-
whenNaN
public static Float whenNaN(Float value, String message, Object arg1, Object... args) throws ArithmeticException Throw an ArithmeticException if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, "Value may not be NaN for object %s with name %s, id %s.", object, name, id);
- Parameters:
value
- Float; the value to check; an exception will be thrown if this is NaNmessage
- String; the message to use in the exception, with formatting identifiersarg1
- Object; 1st value to use for the formatting identifiersargs
- Object...; potential 2nd and further values to use for the formatting identifiers- Returns:
- Float; the value that was passed as the first parameter
- Throws:
ArithmeticException
- if value is NaN
-
whenNaN
public static <T extends Throwable> double whenNaN(double value, Class<T> throwableClass, String message) throws T Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, IllegalArgumentException.class, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, IllegalArgumentException.class, "value");
- Type Parameters:
T
- the Throwable class- Parameters:
value
- double; value to check; an exception will be thrown if the object is NaNthrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- double; the value that was passed as the first parameter
- Throws:
T
- if value is NaN
-
whenNaN
public static <T extends Throwable> float whenNaN(float value, Class<T> throwableClass, String message) throws T Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, IllegalArgumentException.class, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, IllegalArgumentException.class, "value");
- Type Parameters:
T
- the Throwable class- Parameters:
value
- float; value to check; an exception will be thrown if the object is NaNthrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- float; the value that was passed as the first parameter
- Throws:
T
- if value is NaN
-
whenNaN
public static <T extends Throwable> Double whenNaN(Double value, Class<T> throwableClass, String message) throws T Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, IllegalArgumentException.class, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, IllegalArgumentException.class, "value");
- Type Parameters:
T
- the Throwable class- Parameters:
value
- Double; value to check; an exception will be thrown if the object is NaNthrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- Double; the value that was passed as the first parameter
- Throws:
T
- if value is NaN
-
whenNaN
public static <T extends Throwable> Double whenNaN(Double value, Class<T> throwableClass, String message, Object arg1, Object... args) throws T Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, IllegalArgumentException.class, "Value may not be NaN for object %s with name %s, id %s.", object, name, id);
- Type Parameters:
T
- the Throwable class- Parameters:
value
- Double; the value to check; an exception will be thrown if this is NaNthrowableClass
- 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 identifiersargs
- Object...; potential 2nd and further values to use for the formatting identifiers- Returns:
- Double; the value that was passed as the first parameter
- Throws:
T
- if value is NaN
-
whenNaN
public static <T extends Throwable> Float whenNaN(Float value, Class<T> throwableClass, String message) throws T Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, IllegalArgumentException.class, "value may not be NaN.");
A shortened version where the text " may not be NaN" is automatically appended after the variable name is just listing the variable name without any spaces:Throw.whenNaN(value, IllegalArgumentException.class, "value");
- Type Parameters:
T
- the Throwable class- Parameters:
value
- Float; value to check; an exception will be thrown if the object is NaNthrowableClass
- Class<T>; the Throwable type to throwmessage
- String; the message to use in the exception, or the variable name that will be appended with " may not be NaN"- Returns:
- Float; the value that was passed as the first parameter
- Throws:
T
- if value is NaN
-
whenNaN
public static <T extends Throwable> Float whenNaN(Float value, Class<T> throwableClass, String message, Object arg1, Object... args) throws T Throw a specified exception if value is NaN, e.g. for pre- and postcondition checking. Use as follows:
Throw.whenNaN(value, IllegalArgumentException.class, "Value may not be NaN for object %s with name %s, id %s.", object, name, id);
- Type Parameters:
T
- the Throwable class- Parameters:
value
- Float; the value to check; an exception will be thrown if this is NaNthrowableClass
- 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 identifiersargs
- Object...; potential 2nd and further values to use for the formatting identifiers- Returns:
- Float; the value that was passed as the first parameter
- Throws:
T
- if value is NaN
-
throwUnchecked
Throw an unchecked exception for a method with a fixed signature (e.g., extending a method from a library that cannot be changed), without having to declare the exception, which can be impossible when extending a method. The typical use is:@Override public void someMethod() { try { // some code that throws e.g., an IOException } catch IOException e { Throw.throwUnchecked(e); } }
From: http://blog.ragozin.info/2011/10/java-how-to-throw-undeclared-checked.html as mentioned in https://stackoverflow.com/questions/11942946/how-to-throw-an-exception-when-your-method-signature-doesnt-allow-to-throw-exce.- Parameters:
e
- Throwavble; the exception of Throwable to throw in an unchecked manner
-