Skip to content

Instantly share code, notes, and snippets.

@AhmedMourad0
Created August 12, 2020 01:47
Show Gist options
  • Save AhmedMourad0/4a094ffc5509762002dfd3ad50aa1b78 to your computer and use it in GitHub Desktop.
Save AhmedMourad0/4a094ffc5509762002dfd3ad50aa1b78 to your computer and use it in GitHub Desktop.
Code snippets for the `value-based classes and error-handling` Medium article.
data class Password private constructor(val value: String) {
companion object {
fun of(value: String): Password {
return when {
value.isTooShort() -> throw PasswordTooShortException()
value.containsNoNumbers() -> throw PasswordContainsNoNumbersException()
else -> Password(value)
}
}
}
}
class PasswordTooShortException() : Exception("Password is too short!")
class PasswordContainsNoNumbersException() : Exception("Password contains no numbers!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment