Graceful exit
Encyclopedia
A graceful exit is a simple programming idiom
Programming idiom
A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task or algorithm that is not a built-in feature in the programming language being used, or, conversely, the use of an...

 wherein a program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...

 detects a serious error condition and "exits gracefully" in a controlled manner as a result. Often the program prints a descriptive error message
Error message
An error message is information displayed when an unexpected condition occurs, usually on a computer or other device. On modern operating systems with graphical user interfaces, error messages are often displayed using dialog boxes...

 to a terminal
Computer terminal
A computer terminal is an electronic or electromechanical hardware device that is used for entering data into, and displaying data from, a computer or a computing system...

 or log as part of the graceful exit.

Usually, code for a graceful exit exists when the alternative—allowing the error to go undetected and unhandled
Exception handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....

—would produce spurious errors or later anomalous behavior that would be more difficult for the programmer
Programmer
A programmer, computer programmer or coder is someone who writes computer software. The term computer programmer can refer to a specialist in one area of computer programming or to a generalist who writes code for many kinds of software. One who practices or professes a formal approach to...

 to debug. The code associated with a graceful exit may also take additional steps, such as closing files
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

, to ensure that the program leaves data in a consistent, recoverable state.

Graceful exits are not always desired. In many cases, an outright crash
Crash (computing)
A crash in computing is a condition where a computer or a program, either an application or part of the operating system, ceases to function properly, often exiting after encountering errors. Often the offending program may appear to freeze or hang until a crash reporting service documents...

 can give the software developer the opportunity to attach a debugger or collect important information, such as a core dump
Core dump
In computing, a core dump consists of the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally...

 or stack trace
Stack trace
A stack trace is a report of the active stack frames at a certain point in time during the execution of a program.It is commonly used during interactive and post-mortem debugging...

, to diagnose the root cause of the error.

In a language that supports formal exception handling
Exception handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....

, a graceful exit may be the final step in the handling of an exception. In other languages graceful exits can be implemented with additional statements at the locations of possible errors.

In Perl

In the Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

, graceful exits are generally implemented via the die operator. For example, the code for opening a file often reads like the following:


# open the file 'myresults' for writing, or die with an appropriate error message
open RESULTS, '>', 'myresults' or die "can't write to 'myresults' file: $!";


If the attempt to open the file myresults fails, the containing program will terminate with an error message and an exit status
Exit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...

 indicating abnormal termination.

In Java

In the Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 programming language, the try...catch block is used often to catch exceptions. All potentially dangerous code is placed inside the block and, if an exception occurred, is stopped, or caught.


try {
// Try to read the file "file.txt"
Scanner sc = new Scanner(new File("file.txt"));
while(sc.hasNextLine)
System.out.println(sc.readLine);
} catch(IOException e) {
// The file could not be read
System.out.println("The file could not be read. Stack trace:");
e.printStackTrace;
}
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK