All
Thank you for all the hints, I can really use them as I've not used exceptions before.
@Mike: I understand your caveats about speed and keeping the blocks as small as possible. I am making a Monte Carlo simulation for fault tree analysis, and am using exceptions only for the initialisation part where speed does not matter. Currently I need to program the problem directly in the code; I may consider a parser or a GUI later. I have constructed a syntax which should make parsing relatively easy, as it largely accepts strings as parameters.
My toolbox offers a Class TScheduler which encapsulates the whole simulation. The simulation problem is defined by declaring various nodes and connections between them via methods in TScheduler, which holds the nodes in a list. It may look like
Main := TScheduler.Create;
with Main do begin
try //start user defineable section
CompE ('Lamp', 20, 300);
CompE ('Switch', 1000);
Period ('W1'. 80000);
Connect ('Lamp', 'Switch');
....
//end user defineable section
except
on E: Exception do begin
writeln (E.message);
Main.Destroy;
halt;
end;
end;
end; //with main
Main.run;
Main.Destroy;
end.
Most of the individual statements create a specific class instance, but are otherwise independent. Therefore I think it is manageable to raise an exception e.g. in case I entered a nonsense parameter, and let the exception destroy the list and halt. The purpose is for learning really, as a useable program would not work like that but at least offer a parser, where error handling could be done differently.
Well, this works but leaves a few unfreed memory blocks on termination and I cannot find them. Now:
@Bart: Yes I am aware that the code following the raise statement in IntError is never executed, and that brings me back to the original question: By raising an exception, the program jumps directly to the exception statement, and does not even return to the functions that called it. How does the stack survive this ? Who handles the stack in such a case ? The OS? Or does this cause a stack problem which is then the reason for the unfreed memory blocks ?