In java, if one method throws an error, the method that calls it can pass it on to the next method.
public void foo() throws Exception {
throw new Excep
A Swift function can call a throwing function and pass an error
up to the caller, but
throws, andtry.Example:
func foo() throws {
print("in foo")
throw NSError(domain: "mydomain", code: 123, userInfo: nil)
}
func bar() throws -> String {
print("in bar")
try foo()
return "bar"
}
do {
let result = try bar()
print("result:", result)
} catch {
print(error.localizedDescription)
}
Output:
in bar in foo The operation couldn’t be completed. (mydomain error 123.)
If try foo() fails then bar() returns immediately,
propagating the error thrown by foo() to its caller.
In other words, try foo() inside a throwing function is equivalent to
do {
try foo()
} catch let error {
throw error
}