Throwing Errors
suggest changeIf you want a function to be able to throw errors, you need to add the throws keyword after the parentheses that hold the arguments:
func errorThrower() throws -> String {
return ""
}
When you want to throw an error, use throw with a value that conforms to Error:
enum ExampleError: Error {
case failed
}
func errorThrower() throws -> String {
if true {
return "True"
} else {
throw ExampleError.failed
}
}
If you want to call a function that can throw an error, you need to use the try keyword in a do block:
do {
try errorThrower()
}
For more on Swift errors: http://stackoverflow.com/documentation/swift/283/errors#t=201604201738253075092
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents