是否有任何理由爲什麼Apache的(有用的)org.apache.commons.pool.impl.commmons.pool.GenericObjectPool.addObject()被聲明爲拋出異常?爲什麼GenericObjectPool.addObject拋出檢查異常
其實,org.apache.commons.pool.BaseObjectPool宣稱它是這樣,從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;
爲什麼不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;
}
}
您至少需要跟蹤諸如addObjectToPool和destroyObject之類的調用,以查看是否有一個或多個調用了Exception。至於爲什麼?誰知道......但拋出一個RuntimeException也不是一個好主意。 – TofuBeer