Last active
July 31, 2021 10:54
-
-
Save deathlezz/e2c6fb324d993d0b86be3ff8bc21f05d to your computer and use it in GitHub Desktop.
Error handling in Swift 5.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Error handling | |
// | |
// Create error | |
enum InputError: Error { | |
case EmptyName | |
} | |
// Create function | |
func nameInput(name: String) throws -> String { | |
if name.isEmpty { | |
throw InputError.EmptyName | |
} | |
return name | |
} | |
// Call the function | |
do { | |
let user = try nameInput(name: "") | |
print("Created user with name: \(user)") | |
} catch { | |
print("User creation failed with error: \(error)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment