Skip to content

Formatting of empty catch blocks can appear misleading #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamesderlin opened this issue Jun 6, 2021 · 1 comment · Fixed by #1041
Closed

Formatting of empty catch blocks can appear misleading #1029

jamesderlin opened this issue Jun 6, 2021 · 1 comment · Fixed by #1041

Comments

@jamesderlin
Copy link
Contributor

dart format really likes to collapse empty {} blocks onto a single line. However, in the case of a try-catch-finally chain, I think that it would be better not to do that if there is an non-empty block of statements later in the chain. For example:

void main() {
  try {
    doSomething();
  } on Exception catch (e) {
  } finally {
    cleanupSomething();
  }
}

dart format (version 2.13.1 stable) formats it as:

void main() {
  try {
    doSomething();
  } on Exception catch (e) {} finally {
    cleanupSomething();
  }
}

which I think is rather misleading since it appears that cleanupSomething() is called only in response to a thrown Exception.

@munificent
Copy link
Member

Yeah, I agree the formatting isn't ideal here. It should be fairly straightforward to fix.

However, I'll also point out that having an entirely empty catch block without even a comment explaining why you're ignoring an exception might not be ideal code either. :)

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

Successfully merging a pull request may close this issue.

2 participants