Public methods should throw at most one checked exception how to avoid this in java

Hi Experts,

We used Sonar tool  to analyse our Java code and it has this rule
Public methods should throw at most one checked exception(major issue)
how to avoid it
public void delete() throws IOException, PPException {      
  /* ... */
}

PPException extends Exception

i have to use try catch blocks to avoid this or is there any alternative is there.
LVL 2
srikoteshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
You'd need to show us the code inside delete() to get any real help here.

As a trivial workaround you can also modify the signature to be:
public void delete() throws Exception {
}

but that's not really addressing the underlining issue.

Doug
CPColinSenior Java ArchitectCommented:
I see no problem with having a public method that throws more than one checked exception. If you can't easily change your code to agree with Sonar's suggestion, you should change Sonar's configuration so it stops making that suggestion.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dpearsonCommented:
Yeah CPCOlin makes a good point - that's something of a weird coding guideline coming from Sonar.

But people's opinions on checked exceptions can be a bit "sensitive".

That does raise another option - which is converting the checked exception to unchecked.  Personally I hate that (I think checked exceptions are good), but others think it's cool.

If you wish to do it, it's easy:

public void delete() {
  try {
     // Current Code
  } catch (Exception ex) {
     // This is a RuntimeException - so no need to declare it in the signature
     throw new IllegalStateException("Something bad happened",ex) ;
  }
}

Doug
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

CPColinSenior Java ArchitectCommented:
Since Java 7, though, you shouldn't catch Exception in places where you can multi-catch the specific exception types.
dpearsonCommented:
Since Java 7, though, you shouldn't catch Exception in places where you can multi-catch the specific exception types.

True - but not sure that wouldn't be muddying this particular pool a little further :)

Doug
CPColinSenior Java ArchitectCommented:
In a question that starts with the asker trying to make a chunk of code cleaner and use better practices, I'll always offer advice that errs on the side of cleanliness.
srikoteshAuthor Commented:
Excellent
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.