newline-before-throw

Group
Style
Path
lint/style/newline-before-throw
Default
Off, opt in
Origin
Falcon
Rule
279 of 512

Requires a blank line before a throw that follows another statement.

Checks blocks, including nested bodies and closures. First statements, unbraced throws, and switch-case lists are exempt. Blank lines above attached comments count.

Invalid

example.dartdart
void fail(bool condition) {
  print('start');
  throw StateError('failure');
}

void nested(bool condition) {
  if (condition) {
    print('nested');
    throw StateError('nested');
  }
  final callback = () {
    print('closure');
    throw StateError('closure');
  };
  callback();
}

void cases(int value) {
  switch (value) {
    case 0: {
      print(value);
      throw StateError('case block');
    }
    default:
      throw StateError('first');
  }
}

void commented() {
  print('before');
  // No separating blank line.
  throw StateError('failure');
}
Add a blank line before this `throw` statement.
2 print('start');
3 throw StateError('failure');
Add a blank line before this `throw` statement.
8 print('nested');
9 throw StateError('nested');
Add a blank line before this `throw` statement.
12 print('closure');
13 throw StateError('closure');
Add a blank line before this `throw` statement.
21 print(value);
22 throw StateError('case block');
Add a blank line before this `throw` statement.
31 // No separating blank line.
32 throw StateError('failure');

Valid

example.dartdart
void first() {
  throw StateError('first');
}

void separated() {
  print('before');

  throw StateError('after blank line');
}

void nested(bool condition) {
  if (condition) {
    throw StateError('first in nested block');
  }
  final callback = () {
    throw StateError('first in closure');
  };
  callback();
}

void guarded(bool condition) {
  if (condition) throw StateError('unbraced');
}

void cases(int value) {
  switch (value) {
    default:
      throw StateError('first');
  }
}

void followingCaseStatement(int value) {
  switch (value) {
    default:
      print(value);
      throw StateError('case');
  }
}

void commented() {
  print('before');

  // Explain the failure.
  throw StateError('failure');
}

Configure newline-before-throw

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "newline-before-throw": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/newline-before-throw: <reason>