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 {}
When you want to throw an error, use the throw
keyword:
func errorThrower()throws -> String {
if true {
return "True"
} else {
// Throwing an error
throw Error.error
}
}
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