when should I expect Catch not to catch errors?

Post your Gambas programming questions here.
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

when should I expect Catch not to catch errors?

Post by sjsepan »

I have been using Catch in various parts of an app I am writing, and it mostly behaves as I'd expect, but when working with the gb.Settings[], I am running into a Catch that will not catch.

Although I can test for the null value that I am getting back from one particular settings scenario, it is the behavior of Catch that I am concerned with here. In the situation that an error is raised in a procedure that reads Settings, Catch will not be fired, even though Try ... If Error will work.

In the sample case, if I try to read from a non-existing "qq" key, I get nulls, and depending on how the situation is coded, it may or not be trapped. Individual Try ... If Error code will work, but if I try to see the condition in a Catch (or even if I try to re-raise an new error in the If), Catch does not see it.

Please do not get side-tracked by whether my error-handling code is designed the way you would do it. What I am looking for here is an understanding of why Catch does not see the errors.
Thanks in advance!

UPDATE: FYI -- if I originally had a difference between OpenFromSettings1 and OpenFromSettings2, multiple edits to try different approaches ultimately erased the difference before I posted the code, so discount the fact that there are two identical procedures.

CatchSettingsRead.conf:

Code: Select all

[x]
SomeString="strings!"
SomeBoolean=True
SomeInteger=30

[y]
SomeString="str"
SomeBoolean=True
SomeInteger=5

[z]
SomeString="1"
SomeBoolean=True
SomeInteger=2
output:

Code: Select all

False
OpenFromSettings1 not OK
------------------

False
OpenFromSettings2 not OK
------------------

False
Main.OpenFromSettings3.130: Type mismatch: wanted Integer, got Null instead
OpenFromSettings3 not OK

Main.module:
' Gambas module file

Public Sub Main()

    Dim sKey As String = "qq" '"x" "qq"

    If OpenFromSettings1(sKey) Then
        Print "OpenFromSettings1 OK"
    Else
        Print "OpenFromSettings1 not OK"
    Endif

    Print "------------------"

    If OpenFromSettings2(sKey) Then
        Print "OpenFromSettings2 OK"
    Else
        Print "OpenFromSettings2 not OK"
    Endif

    Print "------------------"

    If OpenFromSettings3(sKey) Then
        Print "OpenFromSettings3 OK"
    Else
        Print "OpenFromSettings3 not OK"
    Endif
Catch
    Debug Error.Text
    Print "anything"

End

Public Function OpenFromSettings1(key As String) As Boolean

    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer

    If IsNull(key) Then
        Error.Raise("OpenFromSettings1: Key is null")
    Endif

    sValue = Settings[key & "/SomeString"]
    Print sValue

    bValue = Settings[key & "/SomeBoolean"]
    Print bValue

    iValue = Settings[key & "/SomeInteger"]
    Print iValue

    returnValue = True
Finally
    Return returnValue
Catch
    Debug Error.Text
    Print "anything"

End

Public Function OpenFromSettings2(key As String) As Boolean

    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer

    If IsNull(key) Then
        Error.Raise("OpenFromSettings2: Key is null")
    Endif

    sValue = Settings[key & "/SomeString"]
    Print sValue

    bValue = Settings[key & "/SomeBoolean"]
    Print bValue

    iValue = Settings[key & "/SomeInteger"]
    Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])

    ' Try on statement followed by If Error will see error

    returnValue = True
Finally
    Return returnValue
Catch
    Debug Error.Text
    Print "anything"

End

Public Function OpenFromSettings3(key As String) As Boolean

    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer

    If IsNull(key) Then
        Error.Raise("OpenFromSettings3: Key is null")
    Endif

    Try sValue = Settings[key & "/SomeString"]
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print sValue

    Try bValue = Settings[key & "/SomeBoolean"]
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print bValue

    Try iValue = Settings[key & "/SomeInteger"]
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])

    returnValue = True
Finally
    Return returnValue
    ' Catch
    '     Debug Error.Text
    ' Print "anything"

End
Last edited by sjsepan on Wednesday 30th October 2019 1:24pm, edited 1 time in total.
cage
Posts: 123
Joined: Monday 2nd September 2019 5:47am
Location: Phoenix Arizona

Re: when should I expect Catch not to catch errors?

Post by cage »

You might want to try what was pointed out to me. As far as I have found it catches everything.
  Catch 
    Message(Error.Text)
Try it and see if that helps.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: when should I expect Catch not to catch errors?

Post by stevedee »

sjsepan wrote: Tuesday 29th October 2019 7:51pm ...but when working with the gb.Settings[], I am running into a Catch that will not catch....
That's an interesting problem Steve.

Can you tell me what happens when Catch fails to Catch?

i.e. if you step through the code, one line at a time, does the code just execute one line at a time all the way through and exit in the normal way, or does it jump to Catch but not execute the Catch code, or does it just exit-stage-left?
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: when should I expect Catch not to catch errors?

Post by sjsepan »

cage wrote: Wednesday 30th October 2019 3:18am You might want to try what was pointed out to me. As far as I have found it catches everything.
  Catch 
    Message(Error.Text)
Try it and see if that helps.
@cage,
Thanks for the idea.
So I went back to replace my Debug Error.Text with Message(Error.Text), but the compiler gently reminded me that I was illustrating this behavior in a console app, so I can't use Message without a gui component added. (I originally found it in a gui at running Qt forms, but clearly it's independent of Qt if it happens in the console)
That's actually a good thing, beause the only components in use are gb (required) and gb.Settings.
Steve
Attachments
CatchSettingsRead.zip
(14.99 KiB) Downloaded 364 times
Last edited by sjsepan on Wednesday 30th October 2019 12:44pm, edited 1 time in total.
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: when should I expect Catch not to catch errors?

Post by sjsepan »

stevedee wrote: Wednesday 30th October 2019 10:38am
sjsepan wrote: Tuesday 29th October 2019 7:51pm ...but when working with the gb.Settings[], I am running into a Catch that will not catch....
That's an interesting problem Steve.

Can you tell me what happens when Catch fails to Catch?

i.e. if you step through the code, one line at a time, does the code just execute one line at a time all the way through and exit in the normal way, or does it jump to Catch but not execute the Catch code, or does it just exit-stage-left?
@stevedee,
Good question...
So, stepping through, what I'd typically see, either in the console app that I used to re-create the behavior, or in the Qt gui where I first saw it, is that ...
-it skips past remaining lines between the error location and before the Finally
-it executes the Finally
-it ignores anything in the Catch
-execution returns from the procedure
-and no error is caught in calling procedure either.

SteveS
Last edited by sjsepan on Wednesday 30th October 2019 2:13pm, edited 4 times in total.
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: when should I expect Catch not to catch errors?

Post by sjsepan »

I think my next step is to try to eliminate gb.Settings from the equation.

I have been assuming that since I saw this only in a procedure with reads from Settings, that it may be involved. I need to recode a version of this console app that eliminates the use of the component and simply generates its own error in code (maybe a divide-by-0 or attempting to use a null in an integer)...

UPDATE: I removed the gb.Settings reference, changed the code to assign literal values instead of reading settings, removed the duplicate OpenFromSettings2 (for simplicity). Behavior still present; am I misunderstanding how Catch works?

sjsepan
________________


output:

Code: Select all

>somestring
>T
OpenFromSettings1 not OK
------------------
>somestring
>T
Main.OpenFromSettings3.72: Division by zero
OpenFromSettings3 not OK
Main.module:
' Gambas module file

Public Sub Main()

    If OpenFromSettings1() Then
        Print "OpenFromSettings1 OK"
    Else
        Print "OpenFromSettings1 not OK"
    Endif

    Print "------------------"

    If OpenFromSettings3() Then
        Print "OpenFromSettings3 OK"
    Else
        Print "OpenFromSettings3 not OK"
    Endif
Catch
    Debug Error.Text
    Print "anything"

End

Public Function OpenFromSettings1() As Boolean

    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer

    sValue = "somestring" 'value OK
    Print ">" & sValue

    bValue = True 'value OK
    Print ">" & bValue

    iValue = 1 / 0 'value not OK; same Catch behavior tested w/ 1/0 and Null
    Print ">" & iValue

    returnValue = True
Finally
    Return returnValue
Catch
    Debug Error.Text
    Print "anything"

End

Public Function OpenFromSettings3() As Boolean

    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer

    Try sValue = "somestring" 'value OK
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print ">" & sValue

    Try bValue = True 'value OK
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print ">" & bValue

    Try iValue = 1 / 0 'value not OK; same Catch behavior tested w/ 1/0 and Null
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print ">" & iValue

    returnValue = True
Finally
    Return returnValue
    ' Catch
    '     Debug Error.Text
    ' Print "anything"

End
Attachments
CatchInlineErrors_0-0-6.zip
(15.54 KiB) Downloaded 324 times
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: when should I expect Catch not to catch errors?

Post by cogier »

What is this line all about? Gambas does not seem to like it and I have not seen this type of syntax before.

Image
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: when should I expect Catch not to catch errors?

Post by stevedee »

Steve, I think the problem is your "Finally" says "Return..". so that's what it is doing.

You would normally use Finally to do stuff like close files or destroy objects, but you are saying "whatever happens, return something" so the code does not reach Catch. Does that make sense?

If an error occurs, you don't really want to return anything because the value will probably be wrong. But if you must return a value its better to do something like this:-
...routine
Return x

Finally
 Close files

Catch
 report error
 Return x
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: when should I expect Catch not to catch errors?

Post by stevedee »

Just to underline what I'm saying, if you had code like this:-

Function 1()
    'Do stuff that creates an error

Finally
	GoTo Function 2

Catch
	'report error
End

Function 2()
	'do something else


I think you can see that the Catch code will never execute, because you are specifying a jump around it.

I hope that helps.
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: when should I expect Catch not to catch errors?

Post by sjsepan »

cogier wrote: Wednesday 30th October 2019 2:38pm What is this line all about? Gambas does not seem to like it and I have not seen this type of syntax before.

Image
That was a typo from editing that I didn't catch before posting -- please disregard. I've edited the earlier post to correct it.
Steve
Post Reply