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

Description

Reports a try whose guarded body is empty, and a finally whose cleanup body is empty.

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();
        }
    }
}

Remediation

Put the guarded work back inside the try, or delete the whole statement along with its unreachable handler. For an empty finally, either release what needs releasing - Database.rollback(savepoint), resetting a static re-entry flag - or remove the block.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved