
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
58 lines
1.1 KiB
Plaintext
58 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
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
|
|
|
|
[source,swift]
|
|
----
|
|
let myvar = try! dangerousCode(foo); // Noncompliant
|
|
// ...
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,swift]
|
|
----
|
|
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
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use "try" or "try?" here instead; "try!" disables error propagation.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
``++try!++``
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|