This question already has an answer here:
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
}
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.
Documentation example
The Error Handling documentation has an example like this
enum VendingMachineError: ErrorType {
case InvalidSelection
case InsufficientFunds(centsNeeded: Int)
case OutOfStock
}
and
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.")
}
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.
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.
Update
I would like to do something like this:
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.")
}
What your'e asking in the "update" part is not possible. It's the throwing function decision to let you know what it's may throw, or not to show it, and then you need to handle a generic error.
A lot of functions do reveal information about the thrown errors - for example:
Edit: a possible reason for this - that's way, the throwing function can change the errors it's throws, and all the code that catch those error would still be valid.
NSError
automatically bridges toErrorType
where the domain becomes the type (e.g.NSCocoaErrorDomain
becomesNSCocoaError
) and the error code becomes the value (NSFileReadNoSuchFileError
becomes.FileReadNoSuchFileError
)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 inFoundationErrors.h
(though there are some rare bugs where Foundation can also sometimes return POSIX errors underNSPOSIXErrorDomain
) but these ones might not have been fully bridged so you will have to fall back on managing them at theNSError
level.More information can be found in « Using Swift with Cocoa and Objective-C (Swift 2.2) »
It will return NSError: