@catch block silently swallows the exception
ID |
objectivec.correctness.empty_catch_block |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Error Handling |
Language |
Objective-C |
Description
Reports an @catch clause whose body holds nothing at all, whether the braces are written on one line or across several. A clause that logs, rethrows, or carries a comment explaining why the exception is expected is not reported.
Rationale
An empty @catch turns a failure into silence. The exception is consumed, the method returns as if the work had succeeded, and the caller acts on state that was never produced. The damage surfaces later and somewhere else — a nil result, a stale view, a half-written file — with nothing in the log tying it back to the operation that actually failed, which is the hardest kind of defect to trace back to its origin.
The following code illustrates the pattern detected by this rule:
@implementation Loader
- (void)fetchSwallowing {
@try {
[self load];
// FLAGGED: @catch block silently swallows the exception
} @catch (NSException *e) { }
}
- (void)load {
NSLog(@"loading");
}
- (void)close {
NSLog(@"closing");
}
@end
Remediation
Handle the failure: recover from it, rethrow it with @throw, or at minimum log the exception so the failure leaves a trace. Where the exception genuinely is expected and ignorable, write a comment inside the braces saying which condition raises it and why doing nothing is correct — that also stops this rule from reporting the block.