SonarQube error: Refactor this method to throw at most one checked exception

Viewed 14768

I am using SonarQube and it shows the following error:

Public methods should throw at most one checked exception.

// Noncompliant
public void delete() throws IOException, SQLException { /* ... */ }

// Compliant
public void delete() throws SomeApplicationLevelException { /* ... */ }

Does this means, SomeApplicationLevelException is a parent class and IOException and SQALException are derived from it? And we should throw the parent class exception? Thereby adhering to method throwing only 1 checked exception?

Because I have 2 exceptions that i have defined say for example Exception1 and Exception2 that extend Exception. And my method say, sampleMethod() throws them i.e,

public void sampleMethod() throws Exception1, Exception2 {
}

And the error is shown here. So should I have one class as parent (say MainException) and derive Exception1 and Exception2 from it and throw parent exception class? Like below:

public void sampleMethod() throws MainException {
}

Is the above solution proper?

2 Answers
Related