try! should not be used

Why you should care

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. But there might be some exceptions… And when it does fail, the program will exit abruptly, probably without cleaning up after itself.

How we detect

CAST Highlight counts one occurrence each time a “try!” pattern is used.

Bad Code 

let myvar = try! dangerousCode(foo);  // Noncompliant
// ...

Good Code 

do {
  let myvar = try dangerousCode(foo);
  // ...
} catch {
  // handle error
}

References

5362

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.