Program termination: Difference between revisions
(added Fortran) |
(added haskell) |
||
Line 73: | Line 73: | ||
! message is optional and is a character string. |
! message is optional and is a character string. |
||
! If present, the message is output to the standard output device. |
! If present, the message is output to the standard output device. |
||
=={{header|Haskell}}== |
|||
import Control.Monad |
|||
import System.Exit |
|||
when problem do |
|||
exitWith ExitSuccess (* success *) |
|||
exitWith (ExitFailure integerErrorCode) (* some failure with code *) |
|||
exitFailure (* generic failure *) |
|||
=={{header|Java}}== |
=={{header|Java}}== |
Revision as of 19:46, 20 July 2008
You are encouraged to solve this task according to the task description, using any language you may know.
Show the syntax for a complete stoppage of a program inside a conditional. This includes all threads/processes running under your program.
Ada
Ada programs execute in one or more tasks. All tasks created during the execution of a program depend in a hierarchical manner on the task that create them, except for the environment task which executes the "main" procedure for the program. Each task will abort (terminate abnormally) if the task upon which it depends is aborted. This approach to task termination is not recommended because it does not allow tasks to terminate in a known state.
However, this Rosetta Code task requires a simple stoppage of the program including all tasks. The simple way to achieve this is to abort the environment task.
<ada> with Ada.Task_Identification;
procedure Main is
-- Create as many task objects as your program needs
begin
-- whatever logic is required in your Main procedure if some_condition then Ada.Task_Identification.Abort_Task (Ada.Task_Identification.Current_Task); end if;
end Main; </ada>
A more correct approach to such termination is to provide an entry in each task created by the environment task which, when called by the task upon which it depends, causes the called task to terminate in a known state.
<ada> procedure Main is
-- Create as many task objects as your program needs
begin
-- whatever logic is required in your Main procedure if some_condition then -- for each task created by the Main procedure The_task.Stop; -- end the Main procedure return; -- actually, this is not needed end if;
end Main; </ada>
BASIC
<qbasic>if problem = 1 then
end
end if</qbasic>
C
<c>#include <stdlib.h>
if(problem){
exit(integerErrorCode); /*conventionally, error code 0 is the code for "OK", while anything else is an actual problem*/ /*optionally: return the integerErrorCode from the main() function*/
}</c>
E
Exit indicating successful completion:
if (true) { interp.exitAtTop() }
Exit indicating some problem:
if (true) { interp.exitAtTop("because the task said so") }
Forth
debug @ if QUIT \ quit back to the interpreter else BYE \ exit forth environment completely (e.g. end of a Forth shell script) then
Fortran
IF (condition) STOP [message] ! message is optional and is a character string. ! If present, the message is output to the standard output device.
Haskell
import Control.Monad import System.Exit when problem do exitWith ExitSuccess (* success *) exitWith (ExitFailure integerErrorCode) (* some failure with code *) exitFailure (* generic failure *)
Java
<java>if(problem){
System.exit(integerErrorCode); //conventionally, error code 0 is the code for "OK", while anything else is an actual problem //optionally: Runtime.getRuntime().exit(integerErrorCode);
}</java>
Logo
bye ; exits to shell
throw "toplevel ; exits to interactive prompt
pause ; escapes to interactive prompt for debugging continue ; resumes after a PAUSE
OCaml
<ocaml>if problem then
exit integerErrorCode (*conventionally, error code 0 is the code for "OK",while anything else is an actual problem*)</ocaml>
Perl
<perl>if ($problem) {
exit integerErrorCode; #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case) #while anything else is an actual problem
}</perl>
Pop11
if condition then sysexit(); endif;
Python
<python>if problem:
sys.exit(integerErrorCode) #conventionally, error code 0 is the code for "OK" (you can also omit the argument in this case) #while anything else is an actual problem</python>