#294 indentation - support extra indent for throws clause

Future
closed
nobody
None
5
2014-08-14
2004-04-13
Mike Perham
No

It's pretty standard for a method to have a throws
clause on the next line indented two levels to
differentiate it from the method body:

public static void foo()
throws RemoteException {

for (int i = 0; i < 10; i++) {
    doSomething();
}

}

However the Indentation check forces the throws to line
up with the method body. I would like another config
parameter like the current 3 that would allow for
deeper nesting of the throws clause.

Discussion

  • Nicholas T. Daley

    Logged In: YES
    user_id=853820

    It should also allow less indent than the method body.
    I prefer to have the throws clause starting at the same column as the line
    above it, and the method body indented by 4.

    e.g.


    public static void foo()
    throws RemoteException {
    doSomething();
    }

    as opposed to


    public static void foo()
    throws RemoteException {
    doSomething();
    }

    which is suggested above, or

    public static void foo()
    throws RemoteException {
    doSomething();
    }

    which is what checkstyle currently requires.

     
  • Nicholas T. Daley

    Logged In: YES
    user_id=853820

    Oops, ignore the pre and code tags, I assumed the comment field took html
    tags.

     
  • Comment has been marked as spam. 
    Undo

    You can see all pending comments posted by this user  here

    Anonymous

    Anonymous - 2012-04-17

    This is hitting us too.

    The Checkstyle indentation checker seems to work pretty well overall, especially with Sun conventions, with this one exception:

    public void foo()
    throws Exception {
    // Method body
    }

    Fails with "method def throws at indentation level 16 not at correct indentation, 12". Obviously I don't want to have the "throws Exception" part of the declaration with the same indentation as the method body because it will make the code harder to read - the throws will look like the first line of the method.

    The only workaround I have found is to put the throws on the same line as the method parameters and the exception on the new line, but that's ugly too:

    public void foo() throws
    Exception {
    // Method body
    }

    The workaround implies that this is a bug in Checkstyle's indentation engine. Why would it be happy for "Exception" to be indented twice but "throws Exception" only once?

     
  • Comment has been marked as spam. 
    Undo

    You can see all pending comments posted by this user  here

    Anonymous

    Anonymous - 2012-04-17

    Here's the examples again with hyphens instead of spaces to highlight the indentation, since Sourceforge seems to strip whitespace.

    // This is not allowed - the Checkstyle complains that "throws Exception" should be indented only 4 chars.
    public void foo()
    --------throws Exception {
    ----// Method body
    }

    // This is what Checkstyle wants:
    public void foo()
    ----throws Exception {
    ----// Method body
    }

    // But Checkstyle accepts this, which seems contradictory, a bug?
    public void foo() throws
    --------Exception {
    ----// Method body
    }

     
  • Comment has been marked as spam. 
    Undo

    You can see all pending comments posted by this user  here

    Anonymous

    Anonymous - 2012-04-24
     
  • Sean Flanigan

    Sean Flanigan - 2013-10-01

    I think it would help if the Indentation rule had a relaxed mode, which checked that indentation was an exact multiple of basicOffset, but didn't complain if there were an extra level of indentation.

    Then it wouldn't matter if a throws clause, or a multi-line expression, was indented twice. The main thing is that it is indented, and not with an odd number of spaces.

    It wouldn't be perfect, but in some cases the only alternative is to disable this rule entirely.

     
  • Roman Ivanov

    Roman Ivanov - 2013-12-22
    • status: open --> closed
    • Group: --> Future
     

Log in to post a comment.