Graceful exit

Last updated

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.

Contents

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.openRESULTS,'>','myresults'ordie"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"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

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

intfd;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

Related Research Articles

Bash (Unix shell) GNU replacement for the Bourne shell

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions and all releases of Apple's macOS prior to macOS Catalina. A version is also available for Windows 10 via the Windows Subsystem for Linux. It is also the default user shell in Solaris 11.

In computing, a segmentation fault or access violation is a fault, or failure condition, raised by hardware with memory protection, notifying an operating system (OS) the software has attempted to access a restricted area of memory. On standard x86 computers, this is a form of general protection fault. The OS kernel will, in response, usually perform some corrective action, generally passing the fault on to the offending process by sending the process a signal. Processes can in some cases install a custom signal handler, allowing them to recover on their own, but otherwise the OS default signal handler is used, generally causing abnormal termination of the process, and sometimes a core dump.

In computer science, control flow is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an imperative programming language from a declarative programming language.

In computing and computer programming, exception handling is the process of responding to the occurrence of exceptions – anomalous or exceptional conditions requiring special processing - during the execution of a program. In general, an exception breaks the normal flow of execution and executes a pre-registered exception handler; the details of how this is done depend on whether it is a hardware or software exception and how the software exception is implemented. It is provided by specialized programming language constructs, hardware mechanisms like interrupts, or operating system (OS) inter-process communication (IPC) facilities like signals. Some exceptions, especially hardware ones, may be handled so gracefully that execution can resume where it was interrupted.

In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console, but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.

In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects.

In computer programming, a runtime library is a set of low-level routines used by a compiler to invoke some of the behaviors of a runtime environment, by inserting calls to the runtime library into compiled executable binary. The runtime environment implements the execution model, built-in functions, and other fundamental behaviors of a programming language. During execution of that computer program, execution of those calls to the runtime library cause communication between the executable binary and the runtime environment. A runtime library often includes built-in functions for memory management or exception handling. Therefore, a runtime library is always specific to the platform and compiler.

In computing, a stack trace is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places; the stack and the heap. Memory is continuously allocated on a stack but not on a heap, thus reflective of their names. Stack also refers to a programming construct, thus to differentiate it, this stack is referred to as the program's runtime stack. Technically, once a block of memory has been allocated on the stack, it cannot be easily removed as there can be other blocks of memory that were allocated before it. Each time a function is called in a program, a block of memory is allocated on top of the runtime stack called the activation record At a high level, an activation record allocates memory for the function's parameters and local variables declared in the function.

Resource acquisition is initialization (RAII) is a programming idiom used in several object-oriented, statically-typed programming languages to describe a particular language behavior. In RAII, holding a resource is a class invariant, and is tied to object lifetime: resource allocation is done during object creation, by the constructor, while resource deallocation (release) is done during object destruction, by the destructor. In other words, resource acquisition must succeed for initialization to succeed. Thus the resource is guaranteed to be held between when initialization finishes and finalization starts, and to be held only when the object is alive. Thus if there are no object leaks, there are no resource leaks.

Signals are a limited form of inter-process communication (IPC), typically used in Unix, Unix-like, and other POSIX-compliant operating systems. A signal is an asynchronous notification sent to a process or to a specific thread within the same process to notify it of an event. Signals originated in 1970s Bell Labs Unix and were later specified in the POSIX standard.

A Java compiler is a compiler for the programming language Java. The most common form of output from a Java compiler is Java class files containing platform-neutral Java bytecode, but there are also compilers that output optimized native machine code for a particular hardware/operating system combination.

Abort, Retry, Fail? computer error message

"Abort, Retry, Fail?" is an error message found in DOS operating systems, which prompts the end-user for a course of action to follow. Although welcomed by some, the message also has been cited as an example of poor usability in computer user interfaces.

Exception chaining, or exception wrapping, is an object-oriented programming technique of handling exceptions by re-throwing a caught exception after wrapping it inside a new exception. The original exception is saved as a property of the new exception. The idea is that a method should throw exceptions defined at the same abstraction level as the method itself, but without discarding information from the lower levels.

Exception handling syntax is the set of keywords and/or structures provided by a computer programming language to allow exception handling, which separates the handling of errors that arise during a program's operation from its ordinary processes. Syntax for exception handling varies between programming languages, partly to cover semantic differences but largely to fit into each language's overall syntactic structure. Some languages do not call the relevant concept "exception handling"; others may not have direct facilities for it, but can still provide means to implement it.

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

GNU Smalltalk

GNU Smalltalk is an implementation of the Smalltalk programming language by the GNU Project.

The write is one of the most basic routines provided by a Unix-like operating system kernel. It writes data from a buffer declared by the user to a given device, such as a file. This is the primary way to output data from a program by directly using a system call. The destination is identified by a numeric code. The data to be written, for instance a piece of text, is defined by a pointer and a size, given in number of bytes.

Java bytecode is the instruction set of the Java virtual machine (JVM).

Apache Commons Logging is a Java-based logging utility and a programming model for logging and for other toolkits. It provides APIs, log implementations, and wrapper implementations over some other tools.

PascalABC.NET is a Pascal programming language that implements classic Pascal, most Delphi language features, as well as a number of their own extensions. It is implemented on the .NET Framework platform and contains all the modern language features: classes, operator overloading, interfaces, exception handling, generic classes and routines, garbage collection, lambda expressions, parallel programming tools.

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.