# OK - how do I write this?

**URL:** <https://forum.xojo.com/t/ok-how-do-i-write-this/85310>\
**Category:** Databases\
**Created:** [May 12, 2025, 7:45am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310 "2025-05-12T07:45:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [May 12, 2025, 7:45am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/1 "2025-05-12T07:45:23Z")

</div>

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.:

```auto
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?

---

<div class="post-metadata">

**Author:** ![PaulS](https://forum.xojo.com/user_avatar/forum.xojo.com/pauls/32/199_2.png) [@PaulS](https://forum.xojo.com/u/PaulS)\
**Post date:** [May 12, 2025, 7:56am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/2 "2025-05-12T07:56:06Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [May 12, 2025, 7:56am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/3 "2025-05-12T07:56:09Z")

</div>

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

```

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

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [May 12, 2025, 8:01am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/4 "2025-05-12T08:01:34Z")

</div>

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

What does work is:

```auto
Catch ee as DatabaseException
e = ee
// etc

```

Wierd.

---

<div class="post-metadata">

**Author:** ![PaulS](https://forum.xojo.com/user_avatar/forum.xojo.com/pauls/32/199_2.png) [@PaulS](https://forum.xojo.com/u/PaulS)\
**Post date:** [May 12, 2025, 8:09am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/5 "2025-05-12T08:09:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [May 12, 2025, 8:35am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/6 "2025-05-12T08:35:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [May 12, 2025, 8:35am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/7 "2025-05-12T08:35:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [May 12, 2025, 10:57am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/8 "2025-05-12T10:57:46Z")

</div>

If I say:

`Catch e`

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

---

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [May 12, 2025, 11:14am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/9 "2025-05-12T11:14:38Z")

</div>

> [@TimStreater](#):
>
> 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.

```auto
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:

```auto
// 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

```

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [May 12, 2025, 11:32am UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/10 "2025-05-12T11:32:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [May 12, 2025, 3:28pm UTC](https://forum.xojo.com/t/ok-how-do-i-write-this/85310/11 "2025-05-12T15:28:25Z")

</div>

`Catch e`

is the same as

`var e`

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