Swift Language Functions Throwing Errors


If 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: Errors