SRL Archive - Help and Support

Programming Concept


lordsaturn

2011-03-20 17:50:53

While coding today I realized I wanted to have an exit function for all the procedures in my script, in a hierarchy.

So if my code looks like this:
procedure FindTree;
begin
if not FindColor then
begin
Writeln('FATAL ERROR, KILLING PROCEDURE STACK');
while (sender != CutDownTree) //possible inf loop... getPossibleSenders()?
sender = sender.getSender();
sender.kill();
end;

end;

procedure CutDownTree;
begin
FindTree;
FindTree;
FindTree;
FindTree;
end;

procedure TreeLoop;
begin
CutDownTree;
end;
This allows me to avoid having to add a fail case for every single procedure:
procedure CutDownTree;
begin
if not FindTree then
begin
Writeln('I failed - 1');
Exit;
end;

if not FindTree then
begin
Writeln('I failed - 2');
Exit;
end;

if not FindTree then Exit;
if not FindTree then Exit;
end;

What are your suggestions? I have thought about using exceptions, but that still requires making try/except blocks for every procedure/function.


TL;DR:
procedure a -> procedure b -> procedure c
I want to kill procedure a from procedure c. How?

nielsie95

2011-03-20 18:52:26

That's basically what exceptions do. There's not much else, really.. :p

Rich

2011-03-20 18:56:41

for..to..do?

lordsaturn

2011-03-20 20:37:09

From: Rich
for..to..do?


Explain please? Maybe with an example?

Edit: I think you missed the point.

Nava2

2011-03-20 23:35:50

Throw a custom exception and propagate it as you need it to be done.

lordsaturn

2011-03-21 00:35:30

Yeah exceptions are working out ok… /thread lol

Smartzkid

2011-03-21 01:34:07

I'd recommend exceptions in any other language, but it seems they might be slow(er?) in interpreted pascal - have you noticed any detrimental effects?