Graceful exit

Graceful exit

A graceful exit (or graceful handling) is a simple programming idiom 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.

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.

  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();
  }



Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • exit — {{Roman}}I.{{/Roman}} noun 1 way out ADJECTIVE ▪ back, rear ▪ side ▪ east, south, etc. ▪ He left through the south exit …   Collocations dictionary

  • graceful — adj. Graceful is used with these nouns: ↑arc, ↑arch, ↑curve, ↑exit, ↑finger, ↑motion, ↑move, ↑movement, ↑swan, ↑sweep …   Collocations dictionary

  • Alan Guth — Infobox Scientist name = Alan Harvey Guth caption = Alan Guth at Harvard University birth date = Birth date and age|1947|2|27|df=y birth place = New Brunswick, New Jersey death date = death place = residence = United States nationality = American …   Wikipedia

  • Bob Carr — Infobox President | name =Bob Carr nationality =Australian order =39th Premier of New South Wales term start =25 March 1995 term end =3 August 2005 deputy =Andrew Refshauge predecessor =John Fahey successor =Morris Iemma office2 = New South Wales …   Wikipedia

  • National Collegiate Athletic Association (Philippines) — For other uses, see NCAA (disambiguation). National Collegiate Athletic Association The new NCAA logo introduced in the 2004 05 season Abbreviation NCAA …   Wikipedia

  • Mar Roxas — Manuel Roxas II Secretary of Transportation and Communications Incumbent Assumed office July 4, 2011 President …   Wikipedia

  • ex|it — «EHG ziht, EHK siht», noun, verb. –n. 1. a way out: »The theater had six exits. 2. the act of going out; departure: »When the cat came in, the mouse made a hasty exit. 3. the opportunity to go out. 4. the act of leaving the stage: »The actor made …   Useful english dictionary

  • cosmos — /koz meuhs, mohs/, n., pl. cosmos, cosmoses for 2, 4. 1. the world or universe regarded as an orderly, harmonious system. 2. a complete, orderly, harmonious system. 3. order; harmony. 4. any composite plant of the genus Cosmos, of tropical… …   Universalium

  • Firing — refers to a decision made by an employer to terminate employment. Though such a decision can be made by an employer for a variety of reasons, ranging from an economic downturn to performance related problems on the part of the employee, being… …   Wikipedia

  • Dismissal (employment) — Firing redirects here. For other uses, see Firing (disambiguation). Dismissal (referred to informally as firing or sacking) is the termination of employment by an employer against the will of the employee. Though such a decision can be made by an …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”