== Why is this an issue?

Using `return`, `break`, or `continue` from a `finally` block suppresses the propagation of any unhandled `Exception` which was thrown in the `try` or `catch` block.


This rule raises an issue when a jump statement (`break`, `continue`, `return`) would force control flow to leave a `finally` block.


=== Noncompliant code example

[source,dart]
----
class ReturnInFinally {
  int nonCompliantMethod(int n) {
    for (int i = 0; i < n; ++i) {
      try {
        functionThrowingException(i);
      } catch (e) {
        print(e);
      } finally {
        return 1; // Noncompliant
      }
    }
    return 0;
  }
}
----

[source,dart]
----
class ContinueInFinally {
  int nonCompliantMethod(int n) {
    for (int i = 0; i < n; ++i) {
      try {
        functionThrowingException(i);
      } catch (e) {
        print(e);
      } finally {
        continue; // Noncompliant
      }
    }
    return 0;
  }
}
----

[source,dart]
----
class BreakInFinally {
  int nonCompliantMethod(int n) {
    for (int i = 0; i < n; ++i) {
      try {
        functionThrowingException(i);
      } catch (e) {
        print(e);
      } finally {
        break; // Noncompliant
      }
    }
    return 0;
  }
}
----


=== Compliant solution

[source,dart]
----
class Ok {
  int nonCompliantMethod(int n) {
    for (int i = 0; i < n; ++i) {
      try {
        functionThrowingException(i);
      } catch (e) {
        print(e);
        return 1;
      }
    }
    return 0;
  }
}
----


== Resources

* Dart Docs - https://dart.dev/tools/linter-rules/control_flow_in_finally[Dart Linter rule - control_flow_in_finally]
* CWE - https://cwe.mitre.org/data/definitions/584[CWE-584 - Return Inside Finally Block]
* CERT - https://wiki.sei.cmu.edu/confluence/x/BTdGBQ[CERT, ERR04-J.] - Do not complete abruptly from a finally block

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

* Use of '[return|break|continue]' in a 'finally' clause.

=== Highlighting

The [return|break|continue] statement in the 'finally' block, up to and including `;`.

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
