Avoid catches that only rethrow

Why you should care

A catch clause that only rethrows the caught exception has the same effect as omitting the catch altogether and letting it bubble up automatically, but with more code and the additional detrement of leaving maintainers scratching their heads. Such clauses should either be eliminated or populated with the appropriate logic.

How we detect

CAST Highlight counts one occurrence each time the only instruction of a catch is to rethrow the catched exception. Bad Code
catch (<exception type> <exception name>) {
throw <Exception name> ;
}

References

https://stackoverflow.com/questions/44419928/does-catching-and-rethrowing-the-exact-same-exception-mean-anything https://rules.sonarsource.com/java/RSPEC-2737

About CAST and Highlight’s Code Insights

Over the last 25 years, CAST has leveraged unique knowledge on software quality measurement by analyzing thousands of applications and billions of lines of code. Based on this experience and community standards on programming best practices, Highlight implements hundreds of code insights across 15+ technologies to calculate health factors of a software.
For reference only. For the complete details please refer the original article
https://doc.casthighlight.com/alt_onlyrethrowingcatches-avoid-catches-rethrow/
Have more questions? Submit a request

Comments

Powered by Zendesk