I’m using the VSCode Sonarqube integration.
I get a S2737 warning on the following C++ code:
try { ... }
catch (std::bad_alloc&) {
// bad_alloc needs to escape
throw;
} catch (std::exception const& e) {
actually_handle(e);
}
Since bad_alloc derives from exception, the catch-rethrow is necessary or else the bad_alloc would be caught by the generic handler. But the code is flagged with the message
“Add logic to this catch clause or eliminate it and rethrow the exception automatically.”
This is obviously incorrect advice: all the necessary logic is there, but if I eliminated the clause, the exception wouldn’t be rethrown.
The analysis should ideally understand the relations between the caught classes and not complain about this case. At the very least, as a hack, it should understand that a catch for std::exception or for the wildcard ‘…’ should suppress this warning.