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.

  1. Opening a database file can throw a DatabaseFileLockedException:
  2. ExceptionExample.cs: OpenDatabase
    01private static IObjectContainer OpenDatabase() 02 { 03 IObjectContainer db = null; 04 try 05 { 06 db = Db4oFactory.OpenFile(Db4oFileName); 07 } 08 catch (DatabaseFileLockedException ex) 09 { 10 //System.Console.WriteLine(ex.Message); 11 // ask the user for a new filename, print 12 // or log the exception message 13 // and close the application, 14 // find and fix the reason 15 // and try again 16 } 17 return db; 18 }

    ExceptionExample.vb: OpenDatabase
    01Private Shared Function OpenDatabase() As IObjectContainer 02 Dim db As IObjectContainer = Nothing 03 Try 04 db = Db4oFactory.OpenFile(Db4oFileName) 05 Catch ex As DatabaseFileLockedException 06 ' System.Console.WriteLine(ex.Message) 07 ' ask the user for a new filename, print 08 ' or log the exception message 09 ' and close the application, 10 ' find and fix the reason 11 ' and try again 12 End Try 13 Return db 14 End Function

  3. Opening a client connection can throw IOException:
  4. ExceptionExample.cs: OpenClient
    01private static IObjectContainer OpenClient() 02 { 03 IObjectContainer db = null; 04 try 05 { 06 db = Db4oFactory.OpenClient("host", 0xdb40, "user", "password"); 07 } 08 catch (Exception ex) 09 { 10 //System.Console.WriteLine(ex.Message); 11 // ask the user for new connection details, print 12 // or log the exception message 13 // and close the application, 14 // find and fix the reason 15 // and try again 16 } 17 return db; 18 }

    ExceptionExample.vb: OpenClient
    01Private Shared Function OpenClient() As IObjectContainer 02 Dim db As IObjectContainer = Nothing 03 Try 04 db = Db4oFactory.OpenClient("host", 40, "user", "password") 05 Catch ex As Exception 06 ' System.Console.WriteLine(ex.Message) 07 ' ask the user for a new filename, print 08 ' or log the exception message 09 ' and close the application, 10 ' find and fix the reason 11 ' and try again 12 End Try 13 Return db 14 End Function

  5. Working with db4o and committing a transaction can throw various exceptions; the best practice is to surround your db4o interaction with try-catch block.
  6. ExceptionExample.cs: Work
    01private static void Work() 02 { 03 IObjectContainer db = OpenDatabase(); 04 try 05 { 06 // do some work with db4o 07 db.Commit(); 08 } 09 catch (Db4oException ex) 10 { 11 // handle exception .... 12 } 13 catch (Exception ex) 14 { 15 // handle exception .... 16 } 17 finally 18 { 19 db.Close(); 20 } 21 }

    ExceptionExample.vb: Work
    01Private Shared Sub Work() 02 Dim db As IObjectContainer = OpenDatabase() 03 Try 04 ' do some work with db4o 05 db.Commit() 06 Catch ex As Db4oException 07 ' handle exception .... 08 Catch ex As Exception 09 ' handle exception .... 10 Finally 11 db.Close() 12 End Try 13 End Sub