Help on Try/Catch with DatabaseException

Hello!

I’ve found an odd behaviour, don’t know if it’s a bug or (probably) an error in my code… anyway, here’s the scenario :slight_smile:

Have this code in a thread

Try
  Self.webDB.ExecuteSQL("a-wrong-query")
Catch err As DatabaseException
  System.DebugLog "Error"
  Self.errors.Add err.ErrorNumber.ToString + ": " + err.Message
End Try

Running the thread in debug i see it catching the Exception correctly, however i cannot find any output in the log nor in the errors array (it’s always -1).

webDB is a subclass of MySQLCommunityServer with a custom ExecuteSQL method that is coded like this:

Try
  Super.ExecuteSQL(sqlString)
Catch err As DatabaseException
  If err.ErrorNumber = 2006 Or err.ErrorNumber = 2013 Then
    If Me.connect = False Then Return
    Super.ExecuteSQL(sqlString)
  End If
End Try

Am i doing something wrong?

Thanks.

Are you letting the code run far enough or are you stopping it right after the exception?

If you set a breakpoint in code just after that block, does it reach it, and is the log still empty?

What happens in Self.errors.Add ? :slight_smile:

Kem, yes i set a breakpoing both in System.debugLog and Self.errors.Add line but it never gets reached.

Sascha, i just keep a list of all the errors i get, so i can look after them, in the actual code is a littel more descriptive, i omitted here as it just some informations useful to me :slight_smile:

I assume you have to “Re”-Raise the exception.

Thank you Sascha! I’ve modified my custom class ExecuteSQL method like this

Try
  Super.ExecuteSQL(sqlString)
Catch err As DatabaseException
  If err.ErrorNumber = 2006 Or err.ErrorNumber = 2013 Then
    If Me.connect = False Then Return
    Super.ExecuteSQL(sqlString)
  Else
    Raise New DatabaseException
  End If
End Try

And now it correctly stops at the breack point, writes the log and add the entry to the array.
I’ve learned something new today :slight_smile:

Thanks!

1 Like

FYI, you don’t need to create a new DatabaseException, you can raise the one you got and that will preserve its information.

Else
  Raise err
1 Like

Nice! Thanks Kem :slight_smile: