Loading

'Unreachable Statement' Compile Error in Apex When Using API Version 42.0 or Higher

Fecha de publicación: May 16, 2026
Descripción

The Apex compiler introduced in API Version 42.0 enforces stricter code validation rules, including the detection of unreachable statements and the prevention of mutation of final variables.
In previous versions of the Apex compiler, certain code patterns were allowed even though they contained logical errors — such as incrementing a variable declared as final, or placing executable statements after an unconditional throw statement. These patterns compiled without errors in older API versions.
Starting with API Version 42.0, the Apex compiler correctly identifies and rejects these patterns at compile time. If your Apex class is saved with API Version 42 or higher and contains such code, it will fail to compile with an "Unreachable statement" or similar compile-time error. The purpose of this change is to deprecate a known Apex language bug and improve code quality.

Example of code that no longer compiles in API Version 42.0+:
A variable declared as final integer followed by an increment operation (x++) will produce a compile error because final variables should never be mutated after initialization.

Sample Apex code:

final integer x;
x++; // no longer compiles since finals should never mutate
Solución

Understanding the Behavior Change

Old behavior (pre-API Version 42.0):
The old Apex compiler allowed dead code — that is, code that could never be reached at runtime — to compile without error. For example, a method that throws an exception followed by a call to another method would compile even though the second method call is unreachable. The old compiler also allowed mutation of final variables.

void m() {
throw new MyException();
doSomething();
}


New behavior (API Version 42.0 and later):
The new compiler rejects unreachable statements and mutations of final variables. Code placed after an unconditional throw statement is flagged as unreachable and the class will not compile.

void m() {
if (true) {
throw new MyException();
}
doSomething();
}

How to Fix the Compile Error

To resolve the "Unreachable statement" compile error, use one of the following approaches:
Option 1 — Remove the dead code:
Review the method and remove any statements that can never be reached. This is the recommended approach as it improves code clarity and maintainability.
Option 2 — Use a conditional wrapper (compatibility workaround):
If removing the dead code is not immediately possible, wrap the throw statement in a conditional block such as if (true) { throw new MyException(); }, followed by the originally unreachable statement. This satisfies the new compiler's reachability analysis while preserving the intended logic. Note: This is a workaround for backward compatibility — the best practice is to refactor the code to remove dead code paths entirely.
For final variable mutation errors:
Remove any operations that mutate a final variable after its declaration. A final variable should be assigned only once at the point of declaration. If the variable value needs to change, remove the final keyword.

Recursos adicionales
  • Apex Developer Guide - Compile-Time Errors
  • Apex Release Notes - API Version 42.0
Número del artículo de conocimiento

000382469

 
Cargando
Salesforce Help | Article