orThrow

@ExperimentalSince(version = KotoolsTypesVersion.V4_5_3)
fun orThrow(text: String): EmailAddress

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 method from Kotlin code:

val text = "[email protected]"
val isSuccess: Boolean = try {
EmailAddress.orThrow(text)
true
} catch (exception: IllegalArgumentException) {
false
}
assertTrue(isSuccess)

Calling from Java

Here's an example of calling this method from Java code:

final String text = "[email protected]";
boolean isSuccess;
try {
EmailAddress.orThrow(text);
isSuccess = true;
} catch (final IllegalArgumentException exception) {
isSuccess = false;
}
Assertions.assertTrue(isSuccess);

See the orNull method for returning null instead of throwing an exception in case of invalid text.


@ExperimentalSince(version = KotoolsTypesVersion.V4_5_3)
fun orThrow(text: String, pattern: String): EmailAddress

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 method from Kotlin code:

val text = "[email protected]"
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 method from Java code:

final String text = "[email protected]";
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 method for returning null instead of throwing an exception in case of invalid text or pattern.