What's the equivalent of finally in Swift

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.


defer in Swift 2.0 is like a finally, that means swift ensures you to execute that defer code at the end of current function scope. Here are the some points that i need to know: 1) No matter even guard will returns 2) we can write multiple defer scopes

Here is the example and output that demonstrates multiple defers:

    func myMethod()  {
    print("Message one")
    print("Message two")
    print("Message three")
    defer {
        print("defered block 3")
    }
    defer {
        print("defered block 2")
    }
    defer {
        print("defered block 1")
    }
    print("Message four")
    print("Message five")

}
 Output:
 Message one
 Message two
 Message three
 Message four
 Message five
 defered block 1
 defered block 2
 defered block 3

What you are looking for is called defer. It defines a block of code that is not executed until execution is just about to leave the current scope, but it is always executed.

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

For more details on defer have a look at the Apple Swift documentation, especially section "Specifying Clean-up Actions".