Skip to content

Consistent formatting of multivariable try-with-resource blocks. #1392

Closed

Description

@thatcomputerguy0101

The current formatting of try-with-resource blocks that manage multiple variables is strange, as it results in inconsistent alignment and doesn't match how multiline parameters or code blocks are formatted.

Current output:

try (var input = Files.newInputStream(Path.of("./input"));
    var output = Files.newOutputStream(Path.of("./output")); ) {
  output.write(input.read());
} catch (IOException _) {
}

How I think it should be formatted:

try (
  var input = Files.newInputStream(Path.of("./input"));
  var output = Files.newOutputStream(Path.of("./output"));
) {
  output.write(input.read());
} catch (IOException _) {
}

Activity

  1. cushon commented on Nov 1, 2026

    @cushon
    Collaborator

    The current behaviour is deliberate, the handling of try ( is consistent with other statements that have a fixed-width keyword before (, for example:

    for (int i = 0;
        i < someLongUpperBoundThatForcesWrapping;
        i++) {
      ...
    }
  2. thatcomputerguy0101 commented on Nov 2, 2026

    @thatcomputerguy0101
    Author

    I would say there are different expectations for how statements vs expressions are formatted, although in the case of for, there is a mixture of statements and expressions. In general, statements are aligned to all start in the same column, whereas expressions are often joined by extra symbols that prevent such alignment. While try-with-resource has the first part wrapped in parentheses, it exclusively contains expressions so it should act more like an expression block. (I would question if the formatting you used for your for loop example is optimal, but that is a separate issue.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions