Skip to content

Commit 7951241

Browse files
authored
Replace RuntimeException in catch with Exception to support Kotlin (#971)
Kotlin does not support checked exceptions, so our handlers will use Exception. All handlers are replaced, even those obviously unaffected, for the sake of a consistent noticeable pattern. JAVA-4575
1 parent 463b753 commit 7951241

30 files changed

+60
-65
lines changed

bson/src/test/unit/org/bson/json/JsonReaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ private void testStringAndStream(final String json, final Function<AbstractBsonR
13081308
final Class<? extends RuntimeException> exClass) {
13091309
try {
13101310
testFunc.apply(new JsonReader(json));
1311-
} catch (final RuntimeException e) {
1311+
} catch (final Exception e) {
13121312
if (exClass == null) {
13131313
throw e;
13141314
}
@@ -1317,7 +1317,7 @@ private void testStringAndStream(final String json, final Function<AbstractBsonR
13171317
try {
13181318
testFunc.apply(new JsonReader(new InputStreamReader(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)),
13191319
StandardCharsets.UTF_8)));
1320-
} catch (final RuntimeException e) {
1320+
} catch (final Exception e) {
13211321
if (exClass == null) {
13221322
throw e;
13231323
}

driver-core/src/main/com/mongodb/connection/TlsChannelStreamFactoryFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void start() {
147147
pendingRegistration.attachment);
148148
iter.remove();
149149
}
150-
} catch (IOException | RuntimeException e) {
150+
} catch (Exception e) {
151151
LOGGER.warn("Exception in selector loop", e);
152152
}
153153
}

driver-core/src/main/com/mongodb/internal/async/client/ClientSessionBinding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public int release() {
149149
private void isConnectionSourcePinningRequired(final SingleResultCallback<Boolean> callback) {
150150
try {
151151
callback.onResult(isConnectionSourcePinningRequired(), null);
152-
} catch (RuntimeException e) {
152+
} catch (Exception e) {
153153
callback.onResult(null, e);
154154
}
155155
}

driver-core/src/main/com/mongodb/internal/async/function/RetryState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public void breakAndThrowIfRetryAnd(final Supplier<Boolean> predicate) throws Ru
248248
if (predicate.get()) {
249249
loopState.markAsLastIteration();
250250
}
251-
} catch (RuntimeException predicateException) {
251+
} catch (Exception predicateException) {
252252
predicateException.addSuppressed(localException);
253253
throw predicateException;
254254
}

driver-core/src/main/com/mongodb/internal/async/function/RetryingSyncSupplier.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public R get() {
6565
return syncFunction.get();
6666
} catch (RuntimeException attemptException) {
6767
state.advanceOrThrow(attemptException, failedResultTransformer, retryPredicate);
68+
} catch (Exception attemptException) {
69+
// wrap potential sneaky / Kotlin exceptions
70+
state.advanceOrThrow(new RuntimeException(attemptException), failedResultTransformer, retryPredicate);
6871
}
6972
}
7073
}

driver-core/src/main/com/mongodb/internal/connection/AsynchronousClusterEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private void publishEvents() {
169169
if (isLastEvent) {
170170
break;
171171
}
172-
} catch (RuntimeException | InterruptedException e) {
172+
} catch (Exception e) {
173173
// ignore exceptions thrown from listeners, also ignore interrupts that user code may cause
174174
}
175175
}

driver-core/src/main/com/mongodb/internal/connection/ConcurrentPool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private T createNewAndReleasePermitIfFailure() {
219219
throw new MongoInternalException("The factory for the pool created a null item");
220220
}
221221
return newMember;
222-
} catch (RuntimeException e) {
222+
} catch (Exception e) {
223223
stateAndPermits.releasePermit();
224224
throw e;
225225
}
@@ -277,7 +277,7 @@ public String toString() {
277277
private void close(final T t) {
278278
try {
279279
itemFactory.close(t);
280-
} catch (RuntimeException e) {
280+
} catch (Exception e) {
281281
// ItemFactory.close() really should not throw
282282
}
283283
}

driver-core/src/main/com/mongodb/internal/connection/DefaultConnectionPool.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public InternalConnection get(final long timeoutValue, final TimeUnit timeUnit)
178178
}
179179
connectionPoolListener.connectionCheckedOut(new ConnectionCheckedOutEvent(getId(connection)));
180180
return connection;
181-
} catch (RuntimeException e) {
181+
} catch (Exception e) {
182182
throw (RuntimeException) checkOutFailed(e);
183183
}
184184
}
@@ -201,7 +201,7 @@ public void getAsync(final SingleResultCallback<InternalConnection> callback) {
201201
};
202202
try {
203203
stateAndGeneration.throwIfClosedOrPaused();
204-
} catch (RuntimeException e) {
204+
} catch (Exception e) {
205205
eventSendingCallback.onResult(null, e);
206206
return;
207207
}
@@ -212,7 +212,7 @@ public void getAsync(final SingleResultCallback<InternalConnection> callback) {
212212
PooledConnection connection;
213213
try {
214214
connection = getPooledConnection(timeout);
215-
} catch (RuntimeException e) {
215+
} catch (Exception e) {
216216
eventSendingCallback.onResult(null, e);
217217
return;
218218
}
@@ -375,7 +375,7 @@ ConcurrentPool<UsageTrackingInternalConnection> getPool() {
375375
*/
376376
@VisibleForTesting(otherwise = PRIVATE)
377377
void doMaintenance() {
378-
Predicate<RuntimeException> silentlyComplete = e ->
378+
Predicate<Exception> silentlyComplete = e ->
379379
e instanceof MongoInterruptedException || e instanceof MongoTimeoutException
380380
|| e instanceof MongoConnectionPoolClearedException || ConcurrentPool.isPoolClosedException(e);
381381
try {
@@ -403,7 +403,7 @@ void doMaintenance() {
403403
}
404404
});
405405
}
406-
} catch (RuntimeException e) {
406+
} catch (Exception e) {
407407
if (!silentlyComplete.test(e)) {
408408
LOGGER.warn("Exception thrown during connection pool background maintenance task", e);
409409
throw e;
@@ -532,7 +532,7 @@ public void open() {
532532
try {
533533
connectionCreated(connectionPoolListener, wrapped.getDescription().getConnectionId());
534534
wrapped.open();
535-
} catch (RuntimeException e) {
535+
} catch (Exception e) {
536536
closeAndHandleOpenFailure();
537537
throw new MongoOpenConnectionInternalException(e);
538538
}
@@ -898,7 +898,7 @@ private PooledConnection openWithConcurrencyLimit(final PooledConnection connect
898898
try {//phase one
899899
availableConnection = acquirePermitOrGetAvailableOpenedConnection(
900900
mode == OpenWithConcurrencyLimitMode.TRY_GET_AVAILABLE, timeout);
901-
} catch (RuntimeException e) {
901+
} catch (Exception e) {
902902
connection.closeSilently();
903903
throw e;
904904
}
@@ -938,7 +938,7 @@ void openAsyncWithConcurrencyLimit(
938938
PooledConnection availableConnection;
939939
try {//phase one
940940
availableConnection = acquirePermitOrGetAvailableOpenedConnection(true, timeout);
941-
} catch (RuntimeException e) {
941+
} catch (Exception e) {
942942
connection.closeSilently();
943943
callback.onResult(null, e);
944944
return;
@@ -1300,21 +1300,19 @@ public void close() {
13001300
}
13011301

13021302
private void workerRun() {
1303-
try {
1304-
while (state != State.CLOSED) {
1305-
try {
1306-
Task task = tasks.take();
1307-
if (task.timeout().expired()) {
1308-
task.failAsTimedOut();
1309-
} else {
1310-
task.execute();
1311-
}
1312-
} catch (RuntimeException e) {
1313-
LOGGER.error(null, e);
1303+
while (state != State.CLOSED) {
1304+
try {
1305+
Task task = tasks.take();
1306+
if (task.timeout().expired()) {
1307+
task.failAsTimedOut();
1308+
} else {
1309+
task.execute();
13141310
}
1311+
} catch (InterruptedException closed) {
1312+
// fail the rest of the tasks and stop
1313+
} catch (Exception e) {
1314+
LOGGER.error(null, e);
13151315
}
1316-
} catch (InterruptedException closed) {
1317-
// fail the rest of the tasks and stop
13181316
}
13191317
failAllTasksAfterClosing();
13201318
}

driver-core/src/main/com/mongodb/internal/connection/DefaultDnsSrvRecordMonitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void run() {
8888
try {
8989
dnsSrvRecordInitializer.initialize(unmodifiableSet(hosts));
9090
currentHosts = hosts;
91-
} catch (RuntimeException e) {
91+
} catch (Exception e) {
9292
LOGGER.warn("Exception in monitor thread during notification of DNS resolution state change", e);
9393
}
9494
}
@@ -97,7 +97,7 @@ public void run() {
9797
dnsSrvRecordInitializer.initialize(e);
9898
}
9999
LOGGER.info("Exception while resolving SRV records", e);
100-
} catch (RuntimeException e) {
100+
} catch (Exception e) {
101101
if (currentHosts.isEmpty()) {
102102
dnsSrvRecordInitializer.initialize(new MongoInternalException("Unexpected runtime exception", e));
103103
}

driver-core/src/main/com/mongodb/internal/connection/DefaultServerMonitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ private ServerDescription lookupServerDescription(final ServerDescription curren
230230
elapsedTimeNanos, currentServerDescription.getTopologyVersion() != null));
231231

232232
return createServerDescription(serverId.getAddress(), helloResult, averageRoundTripTime.getAverage());
233-
} catch (RuntimeException e) {
233+
} catch (Exception e) {
234234
serverMonitorListener.serverHeartbeatFailed(
235235
new ServerHeartbeatFailedEvent(connection.getDescription().getConnectionId(), System.nanoTime() - start,
236236
currentServerDescription.getTopologyVersion() != null, e));

0 commit comments

Comments
 (0)