• Post Reply Bookmark Topic Watch Topic
  • New Topic

Signature Declare Throws Exception  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting a code quality error message like "A method/constructor shouldn't explicitly throw java.lang.Exception" when I run PMD. I would really appreciate if someone can help me how I can resove this issue.

My code is simply like below:
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Don't catch the general exception. Only catch the exceptions that can happen.

2) Don't declare your method to throws Exception, only declare it to throw the specific types of exceptions that can happen, and that you re-throw in the catch clause.
 
Ranch Hand
Posts: 226
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Saha wrote:I am getting a code quality error message . . . when I run PMD.


What is PMD?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PMD is a static source code analysis tool - it checks your Java code for common programming problems and bad practices. It's very useful, there are some other tools which do similar things such as FindBugs and Checkstyle.

Sam: the documentation of PMD explains more about the warnings you get. See for example the documentation of the Strict Exceptions rules.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!