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.
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 adefer
block, this is guaranteed to be execute and the end of the scope.Here a sample from SWIFT 2 programming guide