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.
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 }
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
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 }
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
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(); }
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