1. Error Handling
在Swift中,用满足ErrorType协议类型来表示Error。
enum
VendingMachingError:
ErrorType
{
case
InvalidSelection
case
InsufficientFunds(required: Double)
case
OutOfStock
}
为了表明一个函数或者方法会抛出错误,你需要在它的声明之后添加throws关键字,如果不提前声明,则不可抛出错误。
func
canThrowErrors()
throws
->
String
func
cannotThrowErrors() ->
String
在可抛出错误的函数体中的任意位置都可用throw语句抛出错误。
struct
Item {
var price: Double
var count: Int
}
var inventory = [
"Candy Bar" : Item (price: 1.25 , count: 7 ),
"Chips" : Item (price: 1.00 , count: 4 ),
"Pretzels" : Item (price: 0.75 , count: 11 )
]
var amountDeposited = 1.00
func vend(itemNamed name: String ) throws {
guard var item = inventory [name] else {
throw
VendingMachingError . InvalidSelection
}
guard item. count > 0 else {
throw VendingMachingError . OutOfStock
}
if amountDeposited >= item. price {
amountDeposited -= item. price
--item. count
inventory [name] = item
} else {
let amountRequired = item. price - amountDeposited
throw
VendingMachingError . InsufficientFunds (required: amountRequired)
}
var price: Double
var count: Int
}
var inventory = [
"Candy Bar" : Item (price: 1.25 , count: 7 ),
"Chips" : Item (price: 1.00 , count: 4 ),
"Pretzels" : Item (price: 0.75 , count: 11 )
]
var amountDeposited = 1.00
func vend(itemNamed name: String ) throws {
guard var item = inventory [name] else {
throw
VendingMachingError . InvalidSelection
}
guard item. count > 0 else {
throw VendingMachingError . OutOfStock
}
if amountDeposited >= item. price {
amountDeposited -= item. price
--item. count
inventory [name] = item
} else {
let amountRequired = item. price - amountDeposited
throw
VendingMachingError . InsufficientFunds (required: amountRequired)
}
}
在你调用一个可能throw的函数时,你需要在前面加上try关键字。
let
favoriteSnacks = [
"Alice" : "Chips" ,
"Bob" : "Licorice" ,
"Eve" : "Pretzels"
]
func buyFavoriteSnacks(person: String ) throws {
let snackName = favoriteSnacks [person] ?? "Candy Bar"
try vend (itemNamed: snackName)
}
"Alice" : "Chips" ,
"Bob" : "Licorice" ,
"Eve" : "Pretzels"
]
func buyFavoriteSnacks(person: String ) throws {
let snackName = favoriteSnacks [person] ?? "Candy Bar"
try vend (itemNamed: snackName)
}
如果在某些情况下你知道一些可抛出错误的函数在运行时不会抛出错误,你可以使用try!关键字来代替普通的try。
func
willOnlyThrowIfTrue(value:
Bool
)
throws
{
if value {
throw someError
}
do {
try willOnlyThrowIfTrue ( false )
} catch {
//Handle Error
}
}
if value {
throw someError
}
do {
try willOnlyThrowIfTrue ( false )
} catch {
//Handle Error
}
}
try
!
willOnlyThrowIfTrue
(
false
)
defer关键字内的语句直到离开当前代码块时才会被执行。这些语句让你能做一些必要的清理工作,不论当前代码块内有没有错误出现,defer内的语句都会执行。而且defer的执行顺序和它们被定义的顺序是相反的,比如第一个定义的defer会在第二个定义的defer之后执行。
func
processFile(filename:
String
)
throws
{
if exists(filename) {
let file = open(filename)
}
defer {
close (file)
}
while let line = try file.readline() {
//Work with the file.
}
//close(file) is called here, at the end of the scope.
if exists(filename) {
let file = open(filename)
}
defer {
close (file)
}
while let line = try file.readline() {
//Work with the file.
}
//close(file) is called here, at the end of the scope.
}