A Philosophy of Software Design, 2nd Edition by John K. Ousterhout
Author:John K. Ousterhout [Ousterhout, John K.]
Language: eng
Format: epub
Publisher: Yaknyam Press
Published: 2021-07-25T00:00:00+00:00
10.8 Just crash?
The fourth technique for reducing complexity related to exception handling is to crash the application. In most applications there will be certain errors that are not worth trying to handle. Typically, these errors are difficult or impossible to handle and donât occur very often. The simplest thing to do in response to these errors is to print diagnostic information and then abort the application.
One example is âout of memoryâ errors that occur during storage allocation. Consider the malloc function in C, which returns NULL if it cannot allocate the desired block of memory. This is an unfortunate behavior, because it assumes that every single caller of malloc will check the return value and take appropriate action if there is no memory. Applications contain numerous calls to malloc, so checking the result after each call would add significant complexity. If a programmer forgets the check (which is fairly likely), then the application will dereference a null pointer if memory runs out, resulting in a crash that camouflages the real problem.
Furthermore, there isnât much an application can do when it discovers that memory is exhausted. In principle the application could look for unneeded memory to free, but if the application had unneeded memory it could already have freed it, which would have prevented the out-of-memory error in the first place. Todayâs systems have so much memory that memory almost never runs out; if it does, it usually indicates a bug in the application. Thus, it rarely make sense to try to handle out-of-memory errors; this creates too much complexity for too little benefit.
A better approach is to define a new method ckalloc, which calls malloc, checks the result, and aborts the application with an error message if memory is exhausted. The application never invokes malloc directly; it always invokes ckalloc.
In newer languages such as C++ and Java, the new operator throws an exception if memory is exhausted. Thereâs not much point in catching this exception, since thereâs a good chance that the exception handler will also try to allocate memory, which will also fail. Dynamically allocated memory is such a fundamental element of any modern application that it doesnât make sense for the application to continue if memory is exhausted; itâs better to crash as soon as the error is detected.
There are many other examples of errors where crashing the application makes sense. For most programs, if an I/O error occurs while reading or writing an open file (such as a disk hard error), or if a network socket cannot be opened, thereâs not much the application can do to recover, so aborting with a clear error message is a sensible approach. These errors are infrequent, so they are unlikely to affect the overall usability of the application. Aborting with an error message is also appropriate if an application encounters an internal error such as an inconsistent data structure. Conditions like this probably indicate bugs in the program.
Whether or not it is acceptable to crash on a particular error depends on the application.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12517)
Hello! Python by Anthony Briggs(9864)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9754)
The Mikado Method by Ola Ellnestam Daniel Brolund(9744)
Dependency Injection in .NET by Mark Seemann(9290)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8255)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7738)
Grails in Action by Glen Smith Peter Ledbrook(7664)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7513)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6736)
Microservices with Go by Alexander Shuiskov(6499)
Practical Design Patterns for Java Developers by Miroslav Wengner(6402)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6378)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6375)
Angular Projects - Third Edition by Aristeidis Bampakos(5755)
The Art of Crafting User Stories by The Art of Crafting User Stories(5290)
NetSuite for Consultants - Second Edition by Peter Ries(5233)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5049)
Kotlin in Action by Dmitry Jemerov(5016)
