How to find the kind of errors a method may throw

2019-01-23 04:03发布

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.")
}

3条回答
手持菜刀,她持情操
2楼-- · 2019-01-23 04:40

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:

func startVPNTunnel() throws
Description 
Start the process of connecting the VPN
true if the process of connecting the VPN started successfully, false if an error occurred.
Parameters  
error   
A pointer to a pointer to an NSError object. If specified and the VPN connection process cannot be started due to an error, this parameter will be set to point to an NSError object containing details about the error. See NEVPNManager Class Reference for a list of possible errors.

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.

查看更多
Fickle 薄情
3楼-- · 2019-01-23 04:42

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)
}

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.

More information can be found in « Using Swift with Cocoa and Objective-C (Swift 2.2) »

查看更多
Animai°情兽
4楼-- · 2019-01-23 04:54

It will return NSError:

let fileManager = NSFileManager()

do {

    let docsArray = try fileManager.contentsOfDirectoryAtPath("/")

} catch let error as NSError {

    // handle errors
    print(error.localizedDescription)

    // The file “Macintosh HD” couldn’t be opened because you don’t have permission to view it.
}
查看更多
登录 后发表回答