Skip to content

Instantly share code, notes, and snippets.

@deathlezz
Last active July 31, 2021 10:54
Show Gist options
  • Save deathlezz/e2c6fb324d993d0b86be3ff8bc21f05d to your computer and use it in GitHub Desktop.
Save deathlezz/e2c6fb324d993d0b86be3ff8bc21f05d to your computer and use it in GitHub Desktop.
Error handling in Swift 5.
//
// 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