== Why is this an issue?

Catch blocks define how to deal with exceptions. It is possible to partially handle an exception before passing it on to a higher level for complete handling with the empty throw statement `throw;`.

However, when an empty throw is called outside of a catch clause, and there is no exception object to re-throw, the program will call `std::terminate`. This will cause the program to end, which is unlikely to be the expected behavior.

== How to fix it

The rule raises an issue when an empty throw expression is used outside of a catch clause.

=== Code examples

==== Noncompliant code example

[source,cpp]
----
void f(int i) {
  if (i <= 0) {
    throw;   // Noncompliant: it will call std::terminate() if f1 is called while no exception is active
  }
}

void g(int i) {
  try {
    f(i);
    throw; // Noncompliant
  } catch (...) {
    doSomething();
  }
}
----

==== Compliant solution

[source,cpp]
----
void f(int i) {
  try {
    if (i <= 0) {
      throw std::out_of_range("Invalid negative index.");
    }
  } catch (const std::out_of_range& e) { // The catch block handles partially the exception
    std::cout << e.what() << '\n';
    if (i < 0) {
      throw; // And passes control to the next exception handler
    }
  }
}

void g(int i) noexcept {
  try {
    f(i);
  } catch (...) {
    // The catch block handles the re-throw from f
    doSomething();
  }
}
----


== Resources

=== Documentation

* {cpp} reference - https://en.cppreference.com/w/cpp/language/throw[`throw` expression]
* {cpp} reference - https://en.cppreference.com/w/cpp/error/terminate[`std::terminate`]

=== External coding guidelines

* MISRA {cpp}:2008, 15-1-3 - An empty throw (`throw;`) shall only be used in the compound-statement of a catch handler.


ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)

=== relates to: S5747

=== on 31 Mar 2015, 19:04:35 Evgeny Mandrikov wrote:
\[~ann.campbell.2] implementation seems more complete (SQALE, description) than this spec.

=== on 13 Apr 2015, 19:37:22 Evgeny Mandrikov wrote:
\[~ann.campbell.2] I'm wondering why blocker, but not active by default? Note that in implementation currently active.

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