Anda tidak dapat menangkap MongoSecurityException karena dilemparkan ke utas latar belakang.
Anda dapat menunggu MongoTimeoutException untuk menangani 'sinkron':
MongoClientOptions clientOptions = new MongoClientOptions.Builder().serverSelectionTimeout(500).build();
mongoClient = new MongoClient(serverAddress, Collections.singletonList(credential), clientOptions);
try {
String address = mongoClient.getConnectPoint();
System.out.println(address);
}catch (Throwable e){
System.out.println(e);
}
Atau Anda dapat mengimplementasikan ServerListener dan menangani secara asinkron
{
MongoClientOptions clientOptions = new MongoClientOptions.Builder().addServerListener(this).build();
mongoClient = new MongoClient(host1, Collections.singletonList(credential), clientOptions);
}
@Override
public void serverDescriptionChanged(ServerDescriptionChangedEvent event) {
Throwable exception = event.getNewDescription().getException();
handle(exception);
}