Generic Exception Thrown (ARCH4167)
Ce contenu n’est pas encore disponible dans votre langue.
Key java.logging.throw-generic-exception · Severity warning · Category logging
Throwing a generic type such as Exception or RuntimeException forces callers into broad catch blocks and makes it impossible to distinguish different error conditions. Define specific, domain-relevant exceptions.
Incorrect
Section titled “Incorrect”public Order load(String id) { if (id == null) { throw new RuntimeException("id must not be null"); } return repository.find(id);}Correct
Section titled “Correct”public Order load(String id) { if (id == null) { throw new IllegalArgumentException("id must not be null"); } return repository.find(id);}Throw a specific or domain-specific exception type that describes the condition instead of the base/generic type.