Graceful exit

A graceful exit[1] (or graceful handling) is a simple programming idiom[citation needed] wherein a program detects a serious error condition and "exits gracefully" in a controlled manner as a result. Often the program prints a descriptive error message to a terminal 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 — would produce spurious errors or later anomalous behavior that would be more difficult for the programmer to debug. The code associated with a graceful exit may also take additional steps, such as closing files, to ensure that the program leaves data in a consistent, recoverable state.

Graceful exits are not always desired. In many cases, an outright crash can give the software developer the opportunity to attach a debugger or collect important information, such as a core dump or stack trace, to diagnose the root cause of the error.

In a language that supports formal exception handling, 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.

The phrase "graceful exit" has also been generalized to refer to letting go from a job or relationship in life that has ended.[2][3]

In Perl

In the Perl programming language, 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 indicating abnormal termination.

In Java

In the Java 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.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

try {
    // Try to read the file "file.txt"
    Scanner sc = new Scanner(new File("file.txt"));
    while (sc.hasNextLine()) {
        System.out.println(sc.readLine());
    }
    sc.close();
} catch (IOException e) {
    // The file could not be read
    System.err.println("The file could not be read. Stack trace:");
    e.printStackTrace();
}

In C

In C one can use the error(3) function, provided in GNU by the GNU C Library.

#include <errno.h>
#include <error.h>
#include <fcntl.h>

int fd;
if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
    error(1, errno, "Open failed");
}

If the first parameter is non-zero this function will exit from the parent process and return that parameter.

See also

References

  1. ^ "graceful exit". The Free Dictionary. Retrieved September 25, 2016.
  2. ^ Ellen Goodman. "Quote by Ellen Goodman: "There's a trick to the 'graceful exit.' It begi..."". Goodreads. There's a trick to the 'graceful exit.' It begins with the vision to recognize when a job, a life stage, or a relationship is over — and let it go. It means leaving what's over without denying its validity or its past importance to our lives. It involves a sense of future, a belief that every exit line is an entry, that we are moving up, rather than out.
  3. ^ Sue Shellenbarger (August 18, 2015). "How to Leave Your Job Gracefully". The Wall Street Journal. A graceful exit can burnish an employee's reputation and shore up valuable relationships. A bad one can do serious damage to both.


Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.