OK - how do I write this?

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?

Remove the e = Nil
Move dblogmsg (e.ErrorNumber, e.Message, where, sql, args) within Catch.

Catch e as DatabaseException
  app.dbErrorsCount = app.dbErrorsCount + 1
  dblogmsg (e.ErrorNumber, e.Message, where, sql, args)
end try
2 Likes
Catch e as DatabaseException
  app.dbErrorsCount = app.dbErrorsCount + 1
  dblogmsg (e.ErrorNumber, e.Message, where, sql, args)
  Raise e
end try

@PaulS was faster :slight_smile:
I just added a Raise e in case you want to let the Framework handle the exception.

2 Likes

Both of these give me redefined identifier (as e is a byref param for the method) again.

What does work is:

Catch ee as DatabaseException
e = ee
// etc

Wierd.

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.

1 Like

Meaning it’s not possible to pass e through byref into a method and use that in a Catch? Seems a bit of a limitation.

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.

1 Like

If I say:

Catch e

Why does that not refer to the byref e as DatabaseException that is one of my method’s parameters?

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.

3 Likes

Catch e

is the same as

var e

It creates a local variable whose life is within the catch block.

1 Like