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:

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

Post by sjsepan »

stevedee wrote: Wednesday 30th October 2019 3:04pm 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
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.
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 3:21pm 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.
Yes. Very different from .Net, where you can't skip Catch, at least not so easily. :-)
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 3:04pm 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
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
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 »

sjsepan wrote: Wednesday 30th October 2019 4:41pm
stevedee wrote: Wednesday 30th October 2019 3:04pm 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
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
Last edited by sjsepan on Wednesday 30th October 2019 7:02pm, edited 1 time in total.
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: Wednesday 30th October 2019 4:43pm ...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!
Post Reply