Swift3.0基础语法学习<五>

异常处理:

 //
// ViewController5.swift
// SwiftBasicDemo
//
// Created by 思 彭 on 16/11/16.
// Copyright © 2016年 思 彭. All rights reserved. // 错误处理 import UIKit class ViewController5: UIViewController { override func viewDidLoad() {
super.viewDidLoad()
self.title = "错误处理" // 1.函数内的 throws异常处理
func send(job: Int, toPrinter printrName: String) throws ->String {
if printrName == "Never Has Toner" {
throw PrintError.noToner
}
return "Job sent"
} // 2.do...catch处理异常 输出: Job sent
do { let printerresponse = try send(job: , toPrinter: "Bi Sheng")
print(printerresponse)
}catch {
print("error = \(error)")
} // 3.
do {
let printerResponse = try send(job: , toPrinter: "Gutenberg")
print(printerResponse)
} catch PrintError.onFire {
print("I'll just put this over here, with the rest of the fire.")
} catch let printerError as PrintError {
print("Printer error: \(printerError).")
} catch {
print(error)
}
} // 定义“打印错误”的枚举
enum PrintError: Error {
case outOfPaper
case noToner
case onFire
}
}
上一篇:k8s 实验过程中遇到的两个小问题 端口 和 批量删除Error的pods


下一篇:Oracle分页查询和SQL server分页查询总结