Skip to content

New check: JavadocThrowsOrderCheck #19698

@gianmarcoschifone

Description

@gianmarcoschifone

Description

from Oracle Javadoc Tool Guide:

Multiple @throws tags (also known as @exception) should be listed alphabetically by the exception names.

Config

<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
        "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
        "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">
    <module name="TreeWalker">
        <module name="JavadocThrowsOrder"/>
    </module>
</module>

Example

public class Test {

    /**
     * Correct order: alphabetically sorted.
     * @throws IllegalArgumentException if input is invalid
     * @throws NullPointerException if input is null
     */
    public void validOrder1() { }

    /**
     * Incorrect order: N comes after I.
     * @throws NullPointerException if input is null 
     * @throws IllegalArgumentException if input is invalid // violation
     */
    public void invalidOrder1() { }
    
    /**
     * Incorrect order: S comes after I.
     * @exception SecurityException if security check fails
     * @exception IOException if IO error occurs // violation
     */
    public void invalidOrder2() { }

    /**
     * Incorrect order: N comes after I.
     * @exception NullPointerException if input is null 
     * @throws IllegalArgumentException if input is invalid // violation
     */
    public void invalidOrder3() { }
}

Output

$ java -jar checkstyle-13.4.1-all.jar -c config.xml Test.java 
Starting audit...
[ERROR] Test.java:13:8: @throws tag for 'IllegalArgumentException' should be placed alphabetically before 'NullPointerException'. [JavadocThrowsOrder]
[ERROR] Test.java:20:8: @exception tag for 'IOException' should be placed alphabetically before 'SecurityException'. [JavadocThrowsOrder]
[ERROR] Test.java:27:9: @throws tag for 'IllegalArgumentException' should be placed alphabetically before 'NullPointerException'. [JavadocThrowsOrder]
Audit done.
Checkstyle ends with 3 errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions