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
    {
        IObjectContainer container = Db4oEmbedded.OpenFile("database.db4o");
    }
    catch (DatabaseFileLockedException e)
    {
        // Database is already open!
        // Use another database-file or handle this case gracefully
    }
    ImportantExceptionCases.cs: If the database is already open
    Try
        Dim container As IObjectContainer = Db4oEmbedded.OpenFile("database.db4o")
    Catch e As DatabaseFileLockedException
        ' Database is already open!
        ' Use another database-file or handle this case gracefully
    End Try
    ImportantExceptionCases.vb: If the database is already open
  2. Opening a client connection can throw IOException.

    try
    {
        IObjectContainer 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.cs: Cannot connect to the server
    Try
        Dim container As IObjectContainer = Db4oClientServer.OpenClient("localhost", 1337, "sa", "sa")
    Catch e As Db4oIOException
        ' Couldn't connect to the server.
        ' Ask for new connection-settings or handle this case gracefully
    End Try
    ImportantExceptionCases.vb: 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.cs: Violation of the unique constraint
    container.Store(New UniqueId(42))
    container.Store(New UniqueId(42))
    Try
        container.Commit()
    Catch e As UniqueFieldValueConstraintViolationException
        ' Violated the unique-constraint!
        ' Retry with a new value or handle this gracefully
        container.Rollback()
    End Try
    ImportantExceptionCases.vb: Violation of the unique constraint