如何找到一种方法可能在Swift中抛出并捕获的错误 [英] How to find the kind of errors a method may throw and catch them in Swift
问题描述
我正在使用 NSFileManager.contentsOfDirectoryAtPath
来获取目录中的文件名数组。我想使用新的 do-try-catch
语法来处理错误:
I am using NSFileManager.contentsOfDirectoryAtPath
to get an array of file names in a directory. I want to use the new do-try-catch
syntax to handle the errors:
do {
let docsArray = try fileManager.contentsOfDirectoryAtPath(docsPath)
} catch {
// handle errors
print(error) // this is the best I can currently do
}
我可以成像一个错误可能是 docsPath
不存在,但我不知道如何捕获这个错误。我不知道可能发生什么其他错误。
I can imaging that an error might be that the docsPath
doesn't exist, but I don't know how to catch this error. And I don't know what other possible errors might occur.
错误处理文档具有一个这样的例子
enum VendingMachineError: ErrorType {
case InvalidSelection
case InsufficientFunds(centsNeeded: Int)
case OutOfStock
}
和
do {
try vend(itemNamed: "Candy Bar")
// Enjoy delicious snack
} catch VendingMachineError.InvalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let amountNeeded) {
print("Insufficient funds. Please insert an additional \(amountNeeded) cents.")
}
但我不知道如何做类似的事情用于捕捉使用 throws
关键字的方法的标准Swift类型的错误。
but I don't know how to do something similar for catching the errors of the standard Swift types that have methods using the throws
keyword.
NSFileManager contentsOfDirectoryAtPath
的类引用不会说可能返回什么样的错误。所以我不知道要收到什么错误或如何处理它们。
The NSFileManager class reference for contentsOfDirectoryAtPath
doesn't say what kind of errors might be returned. So I don't know what errors to catch or how to handle them if I get them.
我想做这样的事情:
do {
let docsArray = try fileManager.contentsOfDirectoryAtPath(docsPath)
} catch FileManagerError.PathNotFound {
print("The path you selected does not exist.")
} catch FileManagerError.PermissionDenied {
print("You do not have permission to access this directory.")
} catch ErrorType {
print("An error occured.")
}
推荐答案
NSError
自动桥接到 ErrorType
域变为类型(例如 NSCocoaErrorDomain
变为 NSCocoaError
),错误代码变为值( NSFileReadNoSuchFileError
成为 .FileReadNoSuchFileError
)
NSError
automatically bridges to ErrorType
where the domain becomes the type (e.g. NSCocoaErrorDomain
becomes NSCocoaError
) and the error code becomes the value (NSFileReadNoSuchFileError
becomes .FileReadNoSuchFileError
)
import Foundation
let docsPath = "/file/not/found"
let fileManager = NSFileManager()
do {
let docsArray = try fileManager.contentsOfDirectoryAtPath(docsPath)
} catch NSCocoaError.FileReadNoSuchFileError {
print("No such file")
} catch {
// other errors
print(error)
}
至于知道特定调用可以返回哪个错误,只有文档可以帮助。几乎所有的基础错误都是 NSCocoaError
域的一部分,可以在 FoundationErrors.h
中找到(尽管有一些罕见的基金会有时也可能在 NSPOSIXErrorDomain
下返回POSIX错误的错误),但这些错误可能未完全桥接,所以您必须回到管理他们的 NSError
级别。
As for knowing which error can be returned by a specific call, only the documentation can help. Almost all Foundation errors are part of the NSCocoaError
domain and can be found in FoundationErrors.h
(though there are some rare bugs where Foundation can also sometimes return POSIX errors under NSPOSIXErrorDomain
) but these ones might not have been fully bridged so you will have to fall back on managing them at the NSError
level.
更多信息可以在«使用Swift和Cocoa和Objective-C(Swift 2.2)»
这篇关于如何找到一种方法可能在Swift中抛出并捕获的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!