What's the equivalent of finally in Swift

30.6k views Asked by At

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.

5

There are 5 answers

0
Andrea On BEST 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.

0
miho On

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

1
Ashish Kakkad On

Swift 2 introduces its own take on this requirement using the defer keyword

defer { 
    print("Do clean up here") 
}

finally = defer in Swift 2.

Article for defer keyword

0
Arash Jamshidi On

read this : The defer keyword in Swift 2: try/finally done right

for example :

print("Step 1")

do {
    defer { print("Step 2") }
    print("Step 3")
    print("Step 4")
}

print("Step 5")

Output :

Step 1
Step 3
Step 4
Step 2
Step 5
0
Narendra G On

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