when should I expect Catch not to catch errors?
Posted
#1
(In Topic #333)
Regular

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
[x]
SomeString="strings!"
SomeBoolean=True
SomeInteger=30
[y]
SomeString="str"
SomeBoolean=True
SomeInteger=5
[z]
SomeString="1"
SomeBoolean=True
SomeInteger=2
output:
Code
False
OpenFromSettings1 not OK
------------------
False
OpenFromSettings2 not OK
------------------
False
Main.OpenFromSettings3.130: Type mismatch: wanted Integer, got Null instead
OpenFromSettings3 not OK
Main.module:
Code (gambas)
- ' Gambas module file
- Print "OpenFromSettings1 OK"
- Print "OpenFromSettings1 not OK"
- Print "------------------"
- Print "OpenFromSettings2 OK"
- Print "OpenFromSettings2 not OK"
- Print "------------------"
- Print "OpenFromSettings3 OK"
- Print "OpenFromSettings3 not OK"
- Print "anything"
- Print sValue
- Print bValue
- Print iValue
- returnValue = True
- Return returnValue
- Print "anything"
- Print sValue
- Print bValue
- Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])
- ' Try on statement followed by If Error will see error
- returnValue = True
- Return returnValue
- Print "anything"
- Print sValue
- Print bValue
- Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])
- returnValue = True
- Return returnValue
- ' Catch
- ' Debug Error.Text
- ' Print "anything"
Posted
Regular

Posted
Regular

sjsepan said
…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?
Posted
Regular

cage said
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
Posted
Regular

stevedee said
sjsepan said
…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?
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
Posted
Regular

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
>somestring
>T
OpenFromSettings1 not OK
------------------
>somestring
>T
Main.OpenFromSettings3.72: Division by zero
OpenFromSettings3 not OKMain.module:
Code (gambas)
- ' Gambas module file
- Print "OpenFromSettings1 OK"
- Print "OpenFromSettings1 not OK"
- Print "------------------"
- Print "OpenFromSettings3 OK"
- Print "OpenFromSettings3 not OK"
- Print "anything"
- sValue = "somestring" 'value OK
- iValue = 1 / 0 'value not OK; same Catch behavior tested w/ 1/0 and Null
- returnValue = True
- Return returnValue
- Print "anything"
- returnValue = True
- Return returnValue
- ' Catch
- ' Debug Error.Text
- ' Print "anything"
Posted
Guru

<IMG src="http://www.cogier.com/gambas/ambigouous.png">
</IMG>
Posted
Regular

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:-
Posted
Regular

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

cogier said
What is this line all about? Gambas does not seem to like it and I have not seen this type of syntax before.
<IMG src="http://www.cogier.com/gambas/ambigouous.png"></IMG>
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
Posted
Regular

stevedee said
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:-
I see what you're saying. I guess I was used to .Net, which ran the Catch (think of putting out fires) before Finally (leaving and closing the barn door). ;-)
Since a function had to return something, I made sure that there was only one place where it did so.
Posted
Regular

stevedee said
Just to underline what I'm saying, if you had code like this:-
I think you can see that the Catch code will never execute, because you are specifying a jump around it.
I hope that helps.
Yes. Very different from .Net, where you can't skip Catch, at least not so easily. :-)
Posted
Regular

stevedee said
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:-
I see what you're saying. I guess I was used to .Net, which ran the Catch (think of putting out fires) before Finally (leaving and closing the barn door). ;-)
Since a function had to return something, I made sure that there was only one place where it did so.
I made a change as you indicated, and it does indeed NOT skip out without running the catch. Thank You. I will have to adapt my thinking going forward.
Steve
Posted
Regular

sjsepan said
stevedee said
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:-
I see what you're saying. I guess I was used to .Net, which ran the Catch (think of putting out fires) before Finally (stacking and putting things away). ;-)
Since a function had to return something, I made sure that there was only one place where it did so.
I made a change as you indicated, and it does indeed NOT skip out without running the catch. Thank You. I will have to adapt my thinking going forward.
Steve
Posted
Regular

sjsepan said
…I guess I was used to .Net, which ran the Catch (think of putting out fires) before Finally…
Try > Catch > Finally: that's the order for languages like C, PHP, Python, JavaScript….
(….otherwise, why is it called "Finally"?).
Can you think of another language (other than Gambas) where the order is the wrong way around?
So maybe this is the French way!
1 guest and 0 members have just viewed this.


