jweinst1
11/21/2016 - 8:12 AM

error handling in swift 3

error handling in swift 3

//error handling in swift

enum error: Error {
  case SyntaxError
  case ValueError
}

func eval(input:[Int]) throws -> [Int] {
  var newlst = [Int]()
  for elem in input {
    if elem == 0 {
      throw error.ValueError
    }
    else if elem == 2 {
      throw error.SyntaxError
    }
    else {
      newlst.append(elem)
    }
  }
  return newlst
}

print(eval(input:[1, 2, 3]))