Thursday, December 16, 2010

Program 12 - Program Test Data, Debugging and Termination: Program Termination and Return Codes

For this project, I have chosen to compare JAVA, C++, AND C#. Within this section, I will be discussing Program Termination and Return Codes, in relation to the three programming languages.

Normal (program) Termination comes from completing the main function or by calling a function to exit the program.

Within C++, Normal Program Termination can be achieved by calling the function exit.

Exit ();

By calling this function, a value will be returned that will indicate whether or not the program terminated normally.

If the value returned on the exit function is 0, the program has been terminated normally. This will appear as:

Exit (0);

Normal termination in C++ based programs can also be achieved by completing the body of the program, or by executing a return function

Java expresses normal termination in almost the exact same way, give or take a few minor differences. The exit function can be explicitly called with:

System.exit()

Just like in C++, Normal Termination is indicated by a returned value of 0.

C# uses the same concept, but has a call function that differs from both Java and C++.

No matter what the naming convention may be for the call function, the rule behind the value is the same. If it returns a value of 0, the program has terminated correctly.

If the value is anything other than 0, it means that there was an abnormal program termination.

Return Codes are a way to indicate whether a process has completed successfully, or abnormally terminated due to an error. When defining values for a call function, it is a best practice to define the successful completion and normal termination of the process or program with the value of 0.

With this in mind, the programmer can then set other integer values (preferably on the lower scale) for errors that would represent abnormal program termination.

These values cannot simply be read as integers, as they would not be able to give an explanation of why the program abnormally terminated. Return Codes provide context to a return value. An example of this, using JAVA can be seen in the code below:

Prodedure Raise_On_Class

(Class : INTEGER) is

Begin

Case Class is

When 0 => null;

when 1 => raise Status_Error;

when 2 => raise Mode_Error;

when 3 => raise Name_Error;

when 4 => raise Use_Error;

when -1 => raise Date_Error;

when others => raise Unknown_Error;

end case;

end Raise_On_Class;


 

Since these errors will not appear during compilation, this will allow for them to appear within the code once the program is being run. The number of values can be quite vast; however, you may never grab every error. For this reason, it is not uncommon for there to be a "Catch all" value and Return Code. This way, these errors are not assumed as part of a successful termination, but rather an abnormal termination that is outside of the scope of what is already known.

No comments:

Post a Comment