Y a-t-il une bonne raison pour laquelle Apache (utile) org.apache.commons.pool.impl.commmons.pool.GenericObjectPool.addObject() est déclaré pour lancer Exception?Pourquoi GenericObjectPool.addObject est-il vérifié? Exception
En fait, org.apache.commons.pool.BaseObjectPool déclare comme tel, de l'interface org.apache.commons.pool:
/**
* Create an object using the {@link PoolableObjectFactory factory} or other
* implementation dependent mechanism, passivate it, and then place it in the idle object pool.
* <code>addObject</code> is useful for "pre-loading" a pool with idle objects.
* (Optional operation).
*
* @throws Exception when {@link PoolableObjectFactory#makeObject} fails.
* @throws IllegalStateException after {@link #close} has been called on this pool.
* @throws UnsupportedOperationException when this pool cannot add new idle objects.
*/
void addObject() throws Exception, IllegalStateException, UnsupportedOperationException;
Pourquoi pas un dérivé de RuntimeException?
/**
* Create an object, and place it into the pool.
* addObject() is useful for "pre-loading" a pool with idle objects.
*/
@Override
public void addObject() throws Exception {
assertOpen();
if (_factory == null) {
throw new IllegalStateException("Cannot add objects without a factory.");
}
T obj = _factory.makeObject();
try {
assertOpen();
addObjectToPool(obj, false);
} catch (IllegalStateException ex) { // Pool closed
try {
_factory.destroyObject(obj);
} catch (Exception ex2) {
// swallow
}
throw ex;
}
}
Vous auriez besoin, à tout le moins, de suivre les appels comme: addObjectToPool et destroyObject pour voir si un ou plusieurs d'entre eux lance une exception. Quant à pourquoi? Qui sait ... mais lancer une RuntimeException n'est pas une bonne idée non plus. – TofuBeer