Skip to content

Instantly share code, notes, and snippets.

@konrad1977
Created June 12, 2015 12:29
Show Gist options
  • Save konrad1977/2eb3e5928ae746746731 to your computer and use it in GitHub Desktop.
Save konrad1977/2eb3e5928ae746746731 to your computer and use it in GitHub Desktop.
try catch
enum CustomError : ErrorType {
case ErrorWithMessage(message: String)
}
func loginUserWithName(username: String?) throws -> String {
guard let username = username where username.characters.count != 0 else {
throw CustomError.ErrorWithMessage(message: "No username provided")
}
return "token:" + username
}
func login() {
do {
let token = try loginUserWithName(nil)
print("user logged in \(token)")
} catch {
print("An error occurred \(error)")
}
}
login()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment