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
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
' 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