Skip to content

Instantly share code, notes, and snippets.

@amberstar
Last active January 20, 2016 01:54
Show Gist options
  • Save amberstar/7861aee759b5d363d316 to your computer and use it in GitHub Desktop.
Save amberstar/7861aee759b5d363d316 to your computer and use it in GitHub Desktop.
Example of a completion handler that can throw
func divide(numerator: Int, denominator: Int, completion: (() throws -> Int ) -> Void ) {
if numerator == 0 || denominator == 0 {
completion {
throw NSError(domain: "Test", code: 10, userInfo: nil)
}
} else {
completion {
return enumerator / denominator
}
}
}
divide(100, denominator: 0) {
do {
let result = try $0()
print(result)
}
catch {
print("Error")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment