yeah try/catch is very advanced.
yeah try/catch is very advanced.
Regarding Apple, I find it inappropriate (and dishonest) to use the word "advanced", when it is just catching up with a commodity feature. Exceptions are provided by almost every other modern programming language in the world.
That said, the spec here is a bit different than C++ exception I believe.
func loadData() throws { }
func test() {
do {
try loadData()
} catch {
print(error)
}
}
I guess that you have to prefix any statement that might throw with 'try', and cannot prefix other statements with it.If so, their motto really seems to be 'explicit over implicit'. I'm not sure that is an improvement, but I'm not sure that it is bad, either, provided that the refactoring tools work fine (for example when one removes that "throws" from the definition of loadData)
I wonder why they insist on that. Perhaps it is easier to parse?
You need to prefix anything that throws with a "try":
func mightThrow() throws {
try mightAlsoThrow();
}
Other than that, it looks exactly like C++ exceptions: Propagation happens along the chain of functions marked as "throws", until a "catch" handler intercepts it."try" is an expression, by the way, so you can do things like:
let line = try file.readline()
There's also a "try!" statement that throws a runtime exception if the inner statement throws.http://clang.llvm.org/docs/AutomaticReferenceCounting.html#e...