CRITICAL
Rule Definition
An empty catch block defeats the purpose of exceptions.
When an exception occurs, nothing happens and the program fails for an unknown reason. The application can be in an unknown state that will affect subsequent processing.
Since the reason for the issue (the exception type and potential embedded message) are ignored, it will require more time to fix the issue.
Remediation
The exception must be handled correctly according to its type.
Violation Code Sample
do {
try doSomethingWith(person: "Alice")
print("Success! Yum.")
}
catch {
}
Fixed Code Sample
// easy remediation
do {
try doSomethingWith(person: "Alice")
print("Success! Yum.")
}
catch
{
print("Unexpected error:\(error).")
}
// better remediation
do {
try doSomethingWith(person: "Alice")
print("Success! Yum.")
}
catch SomethingError.invalidateError {
print("Invalid error")
}
catch {
print("Unexpected error:\(error).")
}
Reference
https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html
http://cwe.mitre.org/data/definitions/391.html
https://research.tue.nl/en/publications/how-swift-developers-handle-errors
Related Technologies
Technical Criterion
CWE-391 - Unchecked Error Condition
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.