================ @@ -0,0 +1,67 @@ +.. title:: clang-tidy - bugprone-exception-rethrow + +bugprone-exception-rethrow +========================== + +Identifies problematic exception rethrowing, especially with caught exception +variables or empty throw statements outside catch blocks. + +In C++ exception handling, a common pitfall occurs when developers rethrow +caught exceptions within catch blocks by directly passing the caught exception +variable to the ``throw`` statement. While this approach can propagate +exceptions to higher levels of the program, it often leads to code that is less +clear and more error-prone. Rethrowing caught exceptions with the same exception +object within catch blocks can obscure the original context of the exception and +make it challenging to trace program flow. Additionally, this method can +introduce issues such as exception object slicing and performance overhead due +to the invocation of the copy constructor. + +.. code-block:: c++ + + try { + // Code that may throw an exception + } catch (const std::exception& e) { + throw e; // Bad + } + ---------------- isuckatcs wrote:
I think showing an example of exception object slicing would also be benefical if we mention it as an example, so how about presenting the examples like this: ```suggestion introduce issues such as exception object slicing and performance overhead due to the invocation of the copy constructor. .. code-block:: c++ try { // Code that may throw an exception } catch (const std::exception& e) { throw e; // Bad, 'e' is copied } .. code-block:: c++ class derived_exception : public std::exception { ... }; void throwDerived() { throw derived_exception{}; } try { throwDerived(); } catch (const std::exception& e) { throw e; // Bad, exception slicing occurs as 'derived_exception' is rethrown as 'exception' (maybe with better wording ?) } ``` https://github.com/llvm/llvm-project/pull/86448 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits