Обработка ошибок

//do/catch has no "else" branch: the code that
//needs a successful try simply follows it inside
//the same do block.
enum ExceptionError {
    case notANumber
}

func parse(_ text: Stringthrows -> Int {
    guard let number = Int(text) else {
        throw Exception.notANumber
    }
    return number
}

do {
    let number = try parse("42")
    //no exception occurred - we are here
    //number is 42
    print("number is \(number)")
}
catch {
    print("Exception:", error)
}

//defer runs whichever way the block is left
func check(_ text: String) {
    defer {
        print("defer for \(text)")
    }
    guard let number = tryparse(text) else {
        print("not parsed")
        return
    }
    print("parsed \(number)")
}

check("7")
//printed: "parsed 7" and "defer for 7"
check("seven")
//printed: "not parsed" and "defer for seven"