The use of Swift 2.0’s try!
lets you execute code that might throw an exception without using the do
and
catch
syntax normally required for such code. By using it, you’re guaranteeing that the executed code will never fail. Murphy’s Law
guarantees you’re wrong. And when it does fail, the program will exit abruptly, probably without cleaning up after itself.
Noncompliant code example
let myvar = try! dangerousCode(foo); // Noncompliant
// ...
Compliant solution
guard let myvar = try? dangerousCode(foo) else {
// handle error
}
// or
if let myvar = try? dangerousCode(foo); {
// ...
} else {
// handle error
}
// or
do {
let myvar = try dangerousCode(foo)
// ...
} catch {
// handle error
}