Do Not Catch IllegalMonitorStateException

ID

java.no_catch_illegal_monitor_state

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

reliability

Description

Reports catch blocks that catch IllegalMonitorStateException.

Rationale

IllegalMonitorStateException is thrown when wait(), notify(), or notifyAll() is called on an object without holding its monitor (i.e. outside a synchronized block on that object). This is always a programming error. Catching it masks the concurrency bug instead of fixing the synchronization.

// Bad — hides a synchronization bug
try {
    obj.wait();
} catch (IllegalMonitorStateException e) {
    // swallowed
}

Remediation

Ensure the call is inside a properly synchronized block:

// Good — correct synchronization
synchronized (obj) {
    obj.wait();
}