Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Graceful exit

From Wikipedia, the free encyclopedia
Simple programming idiom in a program to detect and manage a serious error condition

Agraceful exit[1] (orgraceful handling) is a simpleprogramming idiom[citation needed] wherein aprogram detects a seriouserror condition and "exits gracefully" in a controlled manner as a result. Often the program prints a descriptiveerror message to aterminal orlog as part of the graceful exit.

Usually, code for a graceful exit exists when the alternative — allowing the error to go undetected andunhandled — would produce spurious errors or later anomalous behavior that would be more difficult for theprogrammer todebug. The code associated with a graceful exit may also take additional steps, such as closingfiles, to ensure that the program leaves data in a consistent, recoverable state.

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

In a language that supports formalexception 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

[edit]

In thePerlprogramming language, graceful exits are generally implemented via thedie 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.openRESULTS,'>','myresults'ordie"can't write to 'myresults' file: $!";

If the attempt to open the filemyresults fails, the containing program will terminate with an error message and anexit status indicating abnormal termination.

In Java

[edit]

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

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

In C

[edit]

InC one can use theerror(3) function, provided inGNU by theGNU C Library.

#include<errno.h>#include<error.h>#include<fcntl.h>if(intfd=open("/dev/urandom",O_RDONLY);fd<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

[edit]

References

[edit]
  1. ^"graceful exit". The Free Dictionary. RetrievedSeptember 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.


Stub icon

Thiscomputer-programming-related article is astub. You can help Wikipedia byadding missing information.

Retrieved from "https://en.wikipedia.org/w/index.php?title=Graceful_exit&oldid=1335332707"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp