0

I've tried to handle MongoDB exceptions with java driver, but in none of my try catch exception are thrown ?

try
{
    MongoClient client = new MongoClient("localhost", 27017);
    DB database = client.getDB("example");
}catch(Exception e){
    e.printStackTrace();
}

Can you help me please ?

David Tavan
  • 69
  • 1
  • 6

1 Answers1

0

You'll get your exception with your first query:

    try
    {
        MongoClient client = new MongoClient("localhost", 27017);
        DB database = client.getDB("example");
        database.getCollection("foo");
    }catch(Exception e){
        e.printStackTrace();
    }

Note: getDB is deprecated, use getDatabase

vmartin
  • 493
  • 4
  • 15
  • I've tried public static void main(String[] args){ try{ MongoClient mongoClient=new MongoClient("localhost",27017); mongoClient.getDatabase("davidyannickdb"); }catch (Exception ex) { System.out.println("Erreur"); } } but my exception is not handled – David Tavan Dec 17 '17 at 13:01
  • shutdown your mongod, try to get a collection (like foo in my code) and you'll see the exception – vmartin Dec 17 '17 at 13:03