Empty try block or empty finally block
ID |
apex.correctness.empty_try_finally_block |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Apex |
Rationale
An empty try guards nothing, so the catch attached to it can never fire - the handler reads as active error handling while being unreachable. An empty finally is the mirror image: it announces cleanup that does not happen, so a reader checking whether a connection, a savepoint or a flag is released finds the block and stops looking. Both shapes are what a refactor leaves behind when the body moves and the scaffolding stays.
The following code illustrates the pattern detected by this rule:
public class TryShapes {
public void emptyTry(Account a) {
// FLAGGED: Empty try block or empty finally block
try {
} catch (DmlException e) {
System.debug(LoggingLevel.ERROR, e);
}
}
public void emptyFinally(Account a) {
// FLAGGED: Empty try block or empty finally block
try {
insert a;
} finally {
}
}
public void bothUsed(Account a) {
try {
insert a;
} catch (DmlException e) {
System.debug(LoggingLevel.ERROR, e);
} finally {
TriggerState.reset();
}
}
}