tounaobun tounaobun - 13 days ago 7
Swift Question

What's the equivalent of finally in Swift

I try to use the error handling modeling in Swift2.

do {
try NSFileManager.defaultManager().removeItemAtPath("path")
} catch {
// ...
} finally {
// compiler error.
}


But it seems that there is no
finally
keyword out there.How can I achieve
try-catch-finally pattern
in Swift.Any help is welcome.

Answer

If you are thinking about the SWIFT 2.0 error handling to be the same thing as exception you are missunderstanding.
This is not exception, this is an error that conforms to a protocol called ErrorType.
The purpose of the block is to intercept the error thrown by a throwing function or method.
Basically there is no finally, what you can do is wrap your code in a defer block, this is guaranteed to be execute and the end of the scope.
Here a sample from SWIFT 2 programming guide

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

You use a defer statement to execute a set of statements just before code execution leaves the current block of code. This lets you do any necessary cleanup that should be performed regardless of whether an error occurred. Examples include closing any open file descriptors and freeing any manually allocated memory.