You are here: Advanced Features > Exception Handling > How To Work With db4o Exceptions

How To Work With db4o Exceptions

Appropriate exception handling will help you to create easy to support systems, saving your time and efforts in the future. The following hints identify important places for exception handling. Take also a look at the list of common db4o exceptions.

  1. Opening a database file can throw a DatabaseFileLockedException.

    try{
        ObjectContainer container = Db4oEmbedded.openFile("database.db4o");
    } catch (DatabaseFileLockedException e){
        // Database is already open!
        // Use another database-file or handle this case gracefully
    }
    ImportantExceptionCases.java: If the database is already open
  2. Opening a client connection can throw IOException.

    try{
        final ObjectContainer container = Db4oClientServer.openClient("localhost", 1337, "sa", "sa");
    
    } catch(Db4oIOException e){
        // Couldn't connect to the server.
        // Ask for new connection-settings or handle this case gracefully
    }
    ImportantExceptionCases.java: Cannot connect to the server
  3. Working with db4o-unique constraints the commit may throw exceptions when the constraints are violated.

    container.store(new UniqueId(42));
    container.store(new UniqueId(42));
    try{
        container.commit();
    } catch (UniqueFieldValueConstraintViolationException e){
        // Violated the unique-constraint!
        // Retry with a new value or handle this gracefully
        container.rollback();
    }
    ImportantExceptionCases.java: Violation of the unique constraint