I am using Ada83 (It's a course requirement to use this version), I am using multiple procedures. I don't know how to come out of the entire program. Some thing like Exit in C program which closes entire procedure. Where is the exit is called from?
-
Assuming no tasking used: can't simple return statement do the job? PS: IMHO it is also quite cruel to force people (especially students) to use Ada 83. – darkestkhan Apr 20 '14 at 13:02
-
@darkestkhan The question isn't about how to return from the main procedure, but how to exit immediately when you're nested several procedures down. Unless I read it wrong. – ajb Apr 21 '14 at 00:29
-
@darkestkhan Yes That's what I mean..thank you – ssr Apr 21 '14 at 04:05
2 Answers
If your program does not use tasks, you can define an exception that signifies an "emergency exit"; perhaps in some package:
package Emergency is
Emergency_Exit : exception;
end Emergency;
In your main procedure, catch this exception:
procedure Your_Main_Procedure is
begin
... whatever the main procedure does
exception
when Emergency.Emergency_Exit =>
null;
end Your_Main_Procedure;
So that whenever you raise the exception somewhere in your program:
raise Emergency_Exit;
it will transfer control to the null
statement, which will then reach the end of the main procedure and exit the program.
Doing it this way means you can add cleanup code to other procedures:
procedure Something_Else is
...
begin
Direct_IO_Instance.Open (Some_File, Direct_IO_Instance.Out_File, "filename");
begin
... do your work
exception
when Emergency.Emergency_Exit =>
-- cleanup
Finish_Writing (Some_File);
Direct_IO_Instance.Close (Some_File);
-- now reraise, which will eventually reach the end of the program
raise;
end;
end Something_Else;
So when Emergency_Exit
is raised, it will eventually transfer control to the end of the main procedure, but it might stop off in other exception handlers along the way to do any needed cleanup.
I don't think this works if there are other tasks running, because the main procedure will wait for those other tasks to complete before the program exits. In that case, in Ada 83, you'll need to coordinate the exit with the other tasks; perhaps you can define a global Boolean that those tasks check periodically to get them to exit, or perhaps you can structure the program in a way so that the Emergency_Exit
exception handler knows what tasks to abort, or can call a task entry to get those tasks to terminate. The best solution would depend on the actual program structure.

- 31,309
- 3
- 58
- 84
On this page there is some explanation on how to do it, and what the risks are: http://rosettacode.org/wiki/Program_termination#Ada

- 23,603
- 7
- 78
- 122
-
The first solution there, which uses `Ada.Task_Identification`, is not available in Ada 83. – ajb Apr 20 '14 at 03:39