Du kan ikke fange MongoSecurityException, da det er smidt i en baggrundstråd.
Du kan vente på, at en MongoTimeoutException håndterer 'synkront':
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);
}
Eller du kan implementere en ServerListener og håndtere asynkront
{
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);
}