So I have the latest Xojo version up and running (2019 r3, Mac OSX El Capitan), and began looking at exception handling. This is in relation to database connection handling, and I noticed the old errorcode and lasterrorcode has now changed.
Previously I called MyDB.Connect and returned the ‘MyDB.errorcode’ or ‘MyDB.databasefile.lasterrorcode’ in a nested If… Then… Else block; if it was zero I worked on the database; if it was non-zero I then called a custom method to provide further information and possible remedial action.
My ‘DBConnect’ method now looks like this (Where zero means success, anything else means we have an issue) :
MyDB = NEW SQLiteDatabase MyDB.DatabaseFile = GetFolderItem("MyDB.sqlite") Dim ExceptionValue As Integer = -1 Try MyDB.Connect Catch DBE As DatabaseException ExceptionValue = DBE.ErrorNumber Finally Return ExceptionValue End Try
The other way I found that works is :
MyDB = NEW SQLiteDatabase MyDB.DatabaseFile = GetFolderItem("MyDB.sqlite") Try MyDB.Connect Catch DBE As DatabaseException Return DBE.ErrorNumber End Try Return 0
I also found out that dimming the ‘ExceptionValue’ can’t be done anywhere in the Try… End Try block, irrespective of whether I include Finally or not.
In terms of handling a database exception when connecting, is the above code reasonable ? My preference is the first one because putting ‘Return 0’ outside of the Try… End Try just feels wrong to me.
PS. Please go easy, this is my first attempt at catching exceptions this way!