I have a class as follows, when I run PMD rule I'm getting a PMD Alert of type AvoidThrowingRawExceptionTypes. I'm not able to resolve this, because when I add any other exception types in constructor throws clause I'm getting error in
new PersistenceManager(CommonConstants.IP_DB)
How to resolve this? Can anybody help me on this?.
public class PersistenceManager implements CommonConstants {
.....
/**
* Stores the persistence mgr for IPMasterData_DB
*/
public static PersistenceManager IPMasterData_DB = new PersistenceManager(
CommonConstants.IP_DB);
/**
* Configures the data source according to the resource passed.
*
* @param dbName
* Databasename
*/
protected PersistenceManager(String dbName) throws Exception{
String resourceName = "";
if (LOGGER == null) {
LOGGER = Logger.getLogger(PersistenceManager.class);
}
try {
resourceName = getConfigFileName(dbName);
this.sessionFactory = new Configuration().configure(resourceName)
.setProperty("hibernate.jdbc.batch_size",
PersistenceManager.getBatchSize(dbName))
.buildSessionFactory();
} catch (HibernateException ex) {
ex.printStackTrace();
LOGGER
.error("Exception building SessionFactory for configKey ",
ex);
throw new RuntimeException(ErrorConstants.SESSIONFACTORY_BUILD, ex);
}
}
}
It is a good programming practice to throw specialized exceptions rather than generic exceptions. PMD can detect and advise you on this and this is exactly what has been done.
Wrap your exceptions in a specialized exception and throw it rather than throwing an instance of RuntimeException.
As I understand, PMD criticizes not the throws clause but throw new RuntimeException(... You can let the throws clause as is but throw a new (more specific) RuntimeException.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With