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 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.


@ExperimentalSince(version = KotoolsTypesVersion.V5_0_1)
fun orThrow(text: String, regex: EmailAddressRegex): EmailAddress

Returns an email address from the specified text, or throws an IllegalArgumentException if the text doesn't match the specified regex.


Calling from Kotlin

Here's an example of calling this function from Kotlin code:

val text = "contact@kotools.org"
val regex: EmailAddressRegex = EmailAddressRegex.alphabetic()
EmailAddress.orThrow(text, regex)

Calling from Java

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

final String text = "contact@kotools.org";
final EmailAddressRegex regex = EmailAddressRegex.alphabetic();
EmailAddress.orThrow(text, regex);

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


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

Deprecated (with error)

Use the 'EmailAddress.Companion.orThrow(String, EmailAddressRegex)' function instead.

Replace with

import org.kotools.types.EmailAddress
import org.kotools.types.EmailAddressRegex
this.orThrow(text, EmailAddressRegex.orThrow(pattern))

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.

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