orThrow
Creates an instance of EmailAddress from the specified text, or throws an IllegalArgumentException if the text doesn't match the default pattern.
Calling from Kotlin
Here's an example of calling this function from Kotlin code:
val text = "contact@kotools.org"
val isSuccess: Boolean = try {
EmailAddress.orThrow(text)
true
} catch (exception: IllegalArgumentException) {
false
}
assertTrue(isSuccess)
Calling from Java
Here's an example of calling this function from Java code:
final String text = "contact@kotools.org";
boolean isSuccess;
try {
EmailAddress.orThrow(text);
isSuccess = true;
} catch (final IllegalArgumentException exception) {
isSuccess = false;
}
Assertions.assertTrue(isSuccess);
See the orNull function for returning null
instead of throwing an exception in case of invalid text.
Creates an instance of EmailAddress from the specified text. Throws an IllegalArgumentException if the text doesn't match the specified pattern, or if the pattern doesn't match the default one.
Calling from Kotlin
Here's an example of calling this function from Kotlin code:
val text = "contact@kotools.org"
val pattern = """^[a-z]+@[a-z]+\.[a-z]+$"""
val isSuccess: Boolean = try {
EmailAddress.orThrow(text, pattern)
true
} catch (exception: IllegalArgumentException) {
false
}
assertTrue(isSuccess)
Calling from Java
Here's an example of calling this function from Java code:
final String text = "contact@kotools.org";
final String pattern = "^[a-z]+@[a-z]+\\.[a-z]+$";
boolean isSuccess;
try {
EmailAddress.orThrow(text, pattern);
isSuccess = true;
} catch (final IllegalArgumentException exception) {
isSuccess = false;
}
Assertions.assertTrue(isSuccess);
See the orNull function for returning null
instead of throwing an exception in case of invalid text or pattern.