-
-
Save amberstar/7861aee759b5d363d316 to your computer and use it in GitHub Desktop.
Example of a completion handler that can throw
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
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