I have a wrapper method to which I pass a DatabaseException paramter byref. And I want to use it to catch the exception, if it happens. I have three ways of writing the Catch. But it seems none is useful.:
Var rega As RowSet
try
e = Nil
rega = dbh.SelectSQL (sql, args)
Return rega // Worked fine, just return
Catch // 1) Doesn't set e
Catch e // 2) Gives me "redefined identifier" e
Catch e as DatabaseException // 3) Also gives me "redefined identifier" e
app.dbErrorsCount = app.dbErrorsCount + 1
end try
dblogmsg (e.ErrorNumber, e.Message, where, sql, args)
Return Nil
.
In case of an exception, I want e to be telling the caller what the error was. How am I supposed to write this?
When you use Catch e as DatabaseException parameter e is a local variable that is only available within the Catch scope.
When you use ee and move the value of ee in e you’re moving the local value to the byref parameter.
Do not define e outside of the try/catch. catch e defines a variable with the same name, creating a collision. Note that e will not be available after end try. It no longer exists. It’s scope is the catch clause only.
Because you define e as a pointer to a variable and then you redefine it in your Catch statement.
You could instead use ByRef err As Integer f.e. and set err to -1 or to e.ErrorNumber as an alternative/workaround.
err = -1
...
Try
...
Catch e as DatabaseException
err = e.ErrorNumber
dblogmsg (e.ErrorNumber, e.Message, where, sql, args)
app.dbErrorsCount = app.dbErrorsCount + 1
// Raise e
End Try
The following seems to work:
// In a Method with the Parameter: ByRef err As RuntimeException
err = Nil
Try
Raise New RuntimeException( "Error Message", 9999 )
Catch e As RuntimeException
err = e
End Try
Don’t ByRef the error. As Sascha suggested, do whatever you need for logging in the catch block, then raise the exception again. The calling code can catch it again.