1
0
mirror of https://github.com/apachecn/eloquent-js-3e-zh.git synced 2025-05-23 20:02:20 +00:00
eloquent-js-3e-zh/diff-en/2ech8-3ech8.diff
wizardforcel 25592d43f2 diff
2018-04-28 14:55:44 +08:00

626 lines
51 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

diff --git a/2ech8.md b/3ech8.md
index bcf3eba..7d57bed 100644
--- a/2ech8.md
+++ b/3ech8.md
@@ -1,28 +1,20 @@
-# Chapter 8Bugs and Error Handling
+# Chapter 8Bugs and Errors
> Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
>
> <footer>Brian Kernighan and P.J. Plauger, <cite>The Elements of Programming Style</cite></footer>
-> Yuan-Ma had written a small program that used many global variables and shoddy shortcuts. Reading it, a student asked, You warned us against these techniques, yet I find them in your program. How can this be?' The master said, There is no need to fetch a water hose when the house is not on fire.'
->
-> <footer>Master Yuan-Ma, <cite>The Book of Programming</cite></footer>
-
-A program is crystallized thought. Sometimes those thoughts are confused. Other times, mistakes are introduced when converting thought into code. Either way, the result is a flawed program.
-
-Flaws in a program are usually called bugs. Bugs can be programmer errors or problems in other systems that the program interacts with. Some bugs are immediately apparent, while others are subtle and might remain hidden in a system for years.
+Flaws in computer programs are usually called _bugs_. It makes programmers feel good to imagine them as little things that just happen to crawl into our work. In reality, of course, we put them there ourselves.
-Often, problems surface only when a program encounters a situation that the programmer didn't originally consider. Sometimes such situations are unavoidable. When the user is asked to input their age and types _orange_, this puts our program in a difficult position. The situation has to be anticipated and handled somehow.
+If a program is crystallized thought, you can roughly categorize bugs into those caused by the thoughts being confused, and those caused by mistakes introduced while converting a thought to code. The former type is generally harder to diagnose and fix than the latter.
-## Programmer mistakes
+## Language
-When it comes to programmer mistakes, our aim is simple. We want to find them and fix them. Such mistakes can range from simple typos that cause the computer to complain as soon as it lays eyes on our program to subtle mistakes in our understanding of the way the program operates, causing incorrect outcomes only in specific situations. Bugs of the latter type can take weeks to diagnose.
+Many mistakes could automatically be pointed out to us by the computer, if it knew enough about what we're trying to do. But here JavaScript's looseness is a hindrance. Its concept of bindings and properties is vague enough that it will rarely catch typos before actually running the program. And even then, it allows you to do some clearly nonsensical things without complaint, such as computing `true * "monkey"`.
-The degree to which languages help you find such mistakes varies. Unsurprisingly, JavaScript is at the “hardly helps at all” end of that scale. Some languages want to know the types of all your variables and expressions before even running a program and will tell you right away when a type is used in an inconsistent way. JavaScript considers types only when actually running the program, and even then, it allows you to do some clearly nonsensical things without complaint, such as `x = true * "monkey"`.
+There are some things that JavaScript does complain about. Writing a program that does not follow the language's grammar will immediately make the computer complain. Other things, such as calling something that's not a function or looking up a property on an undefined value, will cause an error to be reported when the program tries to perform the action.
-There are some things that JavaScript does complain about, though. Writing a program that is not syntactically valid will immediately trigger an error. Other things, such as calling something that's not a function or looking up a property on an undefined value, will cause an error to be reported when the program is running and encounters the nonsensical action.
-
-But often, your nonsense computation will simply produce a `NaN` (not a number) or undefined value. And the program happily continues, convinced that it's doing something meaningful. The mistake will manifest itself only later, after the bogus value has traveled through several functions. It might not trigger an error at all but silently cause the program's output to be wrong. Finding the source of such problems can be difficult.
+But often, your nonsense computation will merely produce `NaN` (not a number) or an undefined value. And the program happily continues, convinced that it's doing something meaningful. The mistake will manifest itself only later, after the bogus value has traveled through several functions. It might not trigger an error at all but silently cause the program's output to be wrong. Finding the source of such problems can be difficult.
The process of finding mistakes—bugs—in programs is called _debugging_.
@@ -33,81 +25,97 @@ JavaScript can be made a _little_ more strict by enabling _strict mode_. This is
```
function canYouSpotTheProblem() {
"use strict";
- for (counter = 0; counter < 10; counter++)
+ for (counter = 0; counter < 10; counter++) {
console.log("Happy happy");
+ }
}
canYouSpotTheProblem();
// → ReferenceError: counter is not defined
```
-Normally, when you forget to put `var` in front of your variable, as with `counter` in the example, JavaScript quietly creates a global variable and uses that. In strict mode, however, an error is reported instead. This is very helpful. It should be noted, though, that this doesn't work when the variable in question already exists as a global variable, but only when assigning to it would have created it.
+Normally, when you forget to put `let` in front of your binding, as with `counter` in the example, JavaScript quietly creates a global binding and uses that. In strict mode an error is reported instead. This is very helpful. It should be noted, though, that this doesn't work when the binding in question already exists as a global binding. In that case, the loop will still quietly overwrite the value of the binding.
-Another change in strict mode is that the `this` binding holds the value `undefined` in functions that are not called as methods. When making such a call outside of strict mode, `this` refers to the global scope object. So if you accidentally call a method or constructor incorrectly in strict mode, JavaScript will produce an error as soon as it tries to read something from `this`, rather than happily working with the global object, creating and reading global variables.
+Another change in strict mode is that the `this` binding holds the value `undefined` in functions that are not called as methods. When making such a call outside of strict mode, `this` refers to the global scope object, which is an object whose properties are the global bindings. So if you accidentally call a method or constructor incorrectly in strict mode, JavaScript will produce an error as soon as it tries to read something from `this`, rather than happily writing to the global scope.
-For example, consider the following code, which calls a constructor without the `new` keyword so that its `this` will _not_ refer to a newly constructed object:
+For example, consider the following code, which calls a constructor function without the `new` keyword so that its `this` will _not_ refer to a newly constructed object:
```
function Person(name) { this.name = name; }
-var ferdinand = Person("Ferdinand"); // oops
+let ferdinand = Person("Ferdinand"); // oops
console.log(name);
// → Ferdinand
```
-So the bogus call to `Person` succeeded but returned an undefined value and created the global variable `name`. In strict mode, the result is different.
+So the bogus call to `Person` succeeded but returned an undefined value and created the global binding `name`. In strict mode, the result is different.
```
"use strict";
function Person(name) { this.name = name; }
-// Oops, forgot 'new'
-var ferdinand = Person("Ferdinand");
+let ferdinand = Person("Ferdinand"); // forgot new
// → TypeError: Cannot set property 'name' of undefined
```
We are immediately told that something is wrong. This is helpful.
-Strict mode does a few more things. It disallows giving a function multiple parameters with the same name and removes certain problematic language features entirely (such as the `with` statement, which is so misguided it is not further discussed in this book).
+Fortunately, constructors created with the `class` notation will always complain if they are called without `new`, making this less of a problem even in non-strict mode.
-In short, putting a `"use strict"` at the top of your program rarely hurts and might help you spot a problem.
+Strict mode does a few more things. It disallows giving a function multiple parameters with the same name and removes certain problematic language features entirely (such as the `with` statement, which is so wrong it is not further discussed in this book).
-## Testing
+In short, putting `"use strict"` at the top of your program rarely hurts and might help you spot a problem.
-If the language is not going to do much to help us find mistakes, we'll have to find them the hard way: by running the program and seeing whether it does the right thing.
+## Types
-Doing this by hand, again and again, is a sure way to drive yourself insane. Fortunately, it is often possible to write a second program that automates testing your actual program.
+Some languages want to know the types of all your bindings and expressions before even running a program. They will tell you right away when a type is used in an inconsistent way. JavaScript considers types only when actually running the program, and even there often tries to implicitly convert values to the type it expects, so it's not much help.
-As an example, we once again use the `Vector` type.
+Still, types provide a useful framework for talking about programs. A lot of mistakes come from being confused about the kind of value that goes into or comes out of a function. If you have that information written down, you're less likely to get confused.
+
+You could add a comment like this above the `goalOrientedRobot` function from the last chapter, to describe its type.
```
-function Vector(x, y) {
- this.x = x;
- this.y = y;
+// (WorldState, Array) → {direction: string, memory: Array}
+function goalOrientedRobot(state, memory) {
+ // ...
}
-Vector.prototype.plus = function(other) {
- return new Vector(this.x + other.x, this.y + other.y);
-};
```
-We will write a program to check that our implementation of `Vector` works as intended. Then, every time we change the implementation, we follow up by running the test program so that we can be reasonably confident that we didn't break anything. When we add extra functionality (for example, a new method) to the `Vector` type, we also add tests for the new feature.
+There are a number of different conventions for annotating JavaScript programs with types.
-```
-function testVector() {
- var p1 = new Vector(10, 20);
- var p2 = new Vector(-10, 5);
- var p3 = p1.plus(p2);
+One thing about types is that they need to introduce their own complexity to be able to describe enough code to be useful. What do you think would be the type of the `randomPick` function that returns a random element from an array? You'd need to introduce a _type variable_, _T_, which can stand in for any type, so that you can give `randomPick` a type like `([T]) → T` (function from an array of _T_s to a _T_).
+
+When the types of a program are known, it is possible for the computer to _check_ them for you, pointing out mistakes before the program is run. There are several JavaScript dialects that add types to the language and check them. The most popular one is called [TypeScript](https://www.typescriptlang.org/). If you are interested in adding more rigor to your programs, I recommend you give it a try.
+
+In this book, we'll continue using raw, dangerous, untyped JavaScript code.
+
+## Testing
+
+If the language is not going to do much to help us find mistakes, we'll have to find them the hard way: by running the program and seeing whether it does the right thing.
+
+Doing this by hand, again and again, is a really bad idea. Not only is it annoying, it also tends to be ineffective, since it takes too much time to exhaustively test everything every time you make a change.
+
+Computers are good at repetitive tasks, and testing is the ideal repetitive task. Automated testing is the process of writing a program that tests another program. Writing tests is a bit more work than testing manually, but once you've done it you gain a kind of superpower: it only takes you a few seconds to verify that your program still behaves properly in all the situations you wrote tests for. When you break something, you'll immediately notice, rather than randomly running into it at some later time.
- if (p1.x !== 10) return "fail: x property";
- if (p1.y !== 20) return "fail: y property";
- if (p2.x !== -10) return "fail: negative x property";
- if (p3.x !== 0) return "fail: x from plus";
- if (p3.y !== 25) return "fail: y from plus";
- return "everything ok";
+Tests usually take the form of little labeled programs that verify some aspect of your code. For example, a set of tests for the (standard, probably already tested by someone else) `toUpperCase` method might look like this:
+
+```
+function test(label, body) {
+ if (!body()) console.log(`Failed: ${label}`);
}
-console.log(testVector());
-// → everything ok
+
+test("convert Latin text to uppercase", () => {
+ return "hello".toUpperCase() == "HELLO";
+});
+test("convert Greek text to uppercase", () => {
+ return "Χαίρετε".toUpperCase() == "ΧΑΊΡΕΤΕ";
+});
+test("don't convert case-less characters", () => {
+ return "مرحبا".toUpperCase() == "مرحبا";
+});
```
-Writing tests like this tends to produce rather repetitive, awkward code. Fortunately, there exist pieces of software that help you build and run collections of tests (_test suites_) by providing a language (in the form of functions and methods) suited to expressing tests and by outputting informative information when a test fails. These are called _testing frameworks_.
+Writing tests like this tends to produce rather repetitive, awkward code. Fortunately, there exist pieces of software that help you build and run collections of tests (_test suites_) by providing a language (in the form of functions and methods) suited to expressing tests and by outputting informative information when a test fails. These are usually called _test runners_.
+
+Some code is easier to test than other code. Generally, the more external objects that the code interacts with, the harder it is to set up the context in which to test it. The style of programming shown in the [previous chapter](07_robot.html), which uses self-contained persistent values rather than changing objects, tends to be easy to test.
## Debugging
@@ -115,13 +123,13 @@ Once you notice that there is something wrong with your program because it misbe
Sometimes it is obvious. The error message will point at a specific line of your program, and if you look at the error description and that line of code, you can often see the problem.
-But not always. Sometimes the line that triggered the problem is simply the first place where a bogus value produced elsewhere gets used in an invalid way. And sometimes there is no error message at all—just an invalid result. If you have been solving the exercises in the earlier chapters, you will probably have already experienced such situations.
+But not always. Sometimes the line that triggered the problem is simply the first place where a flaky value produced elsewhere gets used in an invalid way. If you have been solving the exercises in earlier chapters, you will probably have already experienced such situations.
-The following example program tries to convert a whole number to a string in any base (decimal, binary, and so on) by repeatedly picking out the last digit and then dividing the number to get rid of this digit. But the insane output that it currently produces suggests that it has a bug.
+The following example program tries to convert a whole number to a string in a given base (decimal, binary, and so on) by repeatedly picking out the last digit and then dividing the number to get rid of this digit. But the strange output that it currently produces suggests that it has a bug.
```
-function numberToString(n, base) {
- var result = "", sign = "";
+function numberToString(n, base = 10) {
+ let result = "", sign = "";
if (n < 0) {
sign = "-";
n = -n;
@@ -138,7 +146,7 @@ console.log(numberToString(13, 10));
Even if you see the problem already, pretend for a moment that you don't. We know that our program is malfunctioning, and we want to find out why.
-This is where you must resist the urge to start making random changes to the code. Instead, _think_. Analyze what is happening and come up with a theory of why it might be happening. Then, make additional observations to test this theory—or, if you don't yet have a theory, make additional observations that might help you come up with one.
+This is where you must resist the urge to start making random changes to the code to see if that makes it better. Instead, _think_. Analyze what is happening and come up with a theory of why it might be happening. Then, make additional observations to test this theory—or, if you don't yet have a theory, make additional observations to help you come up with one.
Putting a few strategic `console.log` calls into the program is a good way to get additional information about what the program is doing. In this case, we want `n` to take the values `13`, `1`, and then `0`. Let's write out its value at the start of the loop.
@@ -151,59 +159,72 @@ Putting a few strategic `console.log` calls into the program is a good way to ge
1.5e-323
```
-_Right_. Dividing 13 by 10 does not produce a whole number. Instead of `n /= base`, what we actually want is `n = Math.floor(n / base)` so that the number is properly “shifted” to the right.
+_Right_. Dividing 13 by 10 does not produce a whole number. Instead of `n /= base`, what we actually want is `n = Math.&lt;wbr&gt;floor(n /&lt;wbr&gt; base)` so that the number is properly “shifted” to the right.
-An alternative to using `console.log` is to use the _debugger_ capabilities of your browser. Modern browsers come with the ability to set a _breakpoint_ on a specific line of your code. This will cause the execution of the program to pause every time the line with the breakpoint is reached and allow you to inspect the values of variables at that point. I won't go into details here since debuggers differ from browser to browser, but look in your browser's developer tools and search the Web for more information. Another way to set a breakpoint is to include a `debugger` statement (consisting of simply that keyword) in your program. If the developer tools of your browser are active, the program will pause whenever it reaches that statement, and you will be able to inspect its state.
+An alternative to using `console.log` to peek into the program's behavior is to use the _debugger_ capabilities of your browser. Browsers come with the ability to set a _breakpoint_ on a specific line of your code. When the execution of the program reaches a line with a breakpoint, it is paused, and you can inspect the values of bindings at that point. I won't go into details, as debuggers differ from browser to browser, but look in your browser's developer tools or search the Web for more information.
+
+Another way to set a breakpoint is to include a `debugger` statement (consisting of simply that keyword) in your program. If the developer tools of your browser are active, the program will pause whenever it reaches such a statement.
## Error propagation
-Not all problems can be prevented by the programmer, unfortunately. If your program communicates with the outside world in any way, there is a chance that the input it gets will be invalid or that other systems that it tries to talk to are broken or unreachable.
+Not all problems can be prevented by the programmer, unfortunately. If your program communicates with the outside world in any way, it is possible to get malformed input, to become overloaded with work, or to have the network fail.
-Simple programs, or programs that run only under your supervision, can afford to just give up when such a problem occurs. You'll look into the problem and try again. “Real” applications, on the other hand, are expected to not simply crash. Sometimes the right thing to do is take the bad input in stride and continue running. In other cases, it is better to report to the user what went wrong and then give up. But in either situation, the program has to actively do something in response to the problem.
+If you're only programming for yourself, you can afford to just ignore such problems until they occur. But if you build something that is going to be used by anybody else, you usually want the program to do better than just crash. Sometimes the right thing to do is take the bad input in stride and continue running. In other cases, it is better to report to the user what went wrong and then give up. But in either situation, the program has to actively do something in response to the problem.
-Say you have a function `promptInteger` that asks the user for a whole number and returns it. What should it return if the user inputs _orange_?
+Say you have a function `promptInteger` that asks the user for a whole number and returns it. What should it return if the user inputs “orange”?
-One option is to make it return a special value. Common choices for such values are `null` and `undefined`.
+One option is to make it return a special value. Common choices for such values are `null`, `undefined`, or -1.
```
function promptNumber(question) {
- var result = Number(prompt(question, ""));
- if (isNaN(result)) return null;
+ let result = Number(prompt(question));
+ if (Number.isNaN(result)) return null;
else return result;
}
console.log(promptNumber("How many trees do you see?"));
```
-This is a sound strategy. Now any code that calls `promptNumber` must check whether an actual number was read and, failing that, must somehow recover—maybe by asking again or by filling in a default value. Or it could again return a special value to _its_ caller to indicate that it failed to do what it was asked.
+Now any code that calls `promptNumber` must check whether an actual number was read and, failing that, must somehow recover—maybe by asking again or by filling in a default value. Or it could again return a special value to _its_ caller to indicate that it failed to do what it was asked.
+
+In many situations, mostly when errors are common and the caller should be explicitly taking them into account, returning a special value is a good way to indicate an error. It does, however, have its downsides. First, what if the function can already return every possible kind of value? In such a function, you'll have to do something like wrap the result in an object to be able to distinguish success from failure.
-In many situations, mostly when errors are common and the caller should be explicitly taking them into account, returning a special value is a perfectly fine way to indicate an error. It does, however, have its downsides. First, what if the function can already return every possible kind of value? For such a function, it is hard to find a special value that can be distinguished from a valid result.
+```
+function lastElement(array) {
+ if (array.length == 0) {
+ return {failed: true};
+ } else {
+ return {element: array[array.length - 1]};
+ }
+}
+```
-The second issue with returning special values is that it can lead to some very cluttered code. If a piece of code calls `promptNumber` 10 times, it has to check 10 times whether `null` was returned. And if its response to finding `null` is to simply return `null` itself, the caller will in turn have to check for it, and so on.
+The second issue with returning special values is that it can lead to very awkward code. If a piece of code calls `promptNumber` 10 times, it has to check 10 times whether `null` was returned. And if its response to finding `null` is to simply return `null` itself, callers of the function will in turn have to check for it, and so on.
## Exceptions
-When a function cannot proceed normally, what we would _like_ to do is just stop what we are doing and immediately jump back to a place that knows how to handle the problem. This is what _exception handling_ does.
+When a function cannot proceed normally, what we would _like_ to do is just stop what we are doing and immediately jump to a place that knows how to handle the problem. This is what _exception handling_ does.
-Exceptions are a mechanism that make it possible for code that runs into a problem to _raise_ (or _throw_) an exception, which is simply a value. Raising an exception somewhat resembles a super-charged return from a function: it jumps out of not just the current function but also out of its callers, all the way down to the first call that started the current execution. This is called _unwinding the stack_. You may remember the stack of function calls that was mentioned in [Chapter 3](03_functions.html#stack). An exception zooms down this stack, throwing away all the call contexts it encounters.
+Exceptions are a mechanism that makes it possible for code that runs into a problem to _raise_ (or _throw_) an exception. An exception can be any value. Raising one somewhat resembles a super-charged return from a function: it jumps out of not just the current function but also out of its callers, all the way down to the first call that started the current execution. This is called _unwinding the stack_. You may remember the stack of function calls that was mentioned in [Chapter 3](03_functions.html#stack). An exception zooms down this stack, throwing away all the call contexts it encounters.
-If exceptions always zoomed right down to the bottom of the stack, they would not be of much use. They would just provide a novel way to blow up your program. Their power lies in the fact that you can set “obstacles” along the stack to _catch_ the exception as it is zooming down. Then you can do something with it, after which the program continues running at the point where the exception was caught.
+If exceptions always zoomed right down to the bottom of the stack, they would not be of much use. They'd just provide a novel way to blow up your program. Their power lies in the fact that you can set “obstacles” along the stack to _catch_ the exception as it is zooming down. Once you've caught an exception, you can do something with it to address the problem, and then continue to run the program.
Here's an example:
```
function promptDirection(question) {
- var result = prompt(question, "");
+ let result = prompt(question);
if (result.toLowerCase() == "left") return "L";
if (result.toLowerCase() == "right") return "R";
throw new Error("Invalid direction: " + result);
}
function look() {
- if (promptDirection("Which way?") == "L")
+ if (promptDirection("Which way?") == "L") {
return "a house";
- else
+ } else {
return "two angry bears";
+ }
}
try {
@@ -213,85 +234,97 @@ try {
}
```
-The `throw` keyword is used to raise an exception. Catching one is done by wrapping a piece of code in a `try` block, followed by the keyword `catch`. When the code in the `try` block causes an exception to be raised, the `catch` block is evaluated. The variable name (in parentheses) after `catch` will be bound to the exception value. After the `catch` block finishes—or if the `try` block finishes without problems—control proceeds beneath the entire `try/catch` statement.
+The `throw` keyword is used to raise an exception. Catching one is done by wrapping a piece of code in a `try` block, followed by the keyword `catch`. When the code in the `try` block causes an exception to be raised, the `catch` block is evaluated, with the name in parentheses bound to the exception value. After the `catch` block finishes—or if the `try` block finishes without problems—the program proceeds beneath the entire `try/catch` statement.
-In this case, we used the `Error` constructor to create our exception value. This is a standard JavaScript constructor that creates an object with a `message` property. In modern JavaScript environments, instances of this constructor also gather information about the call stack that existed when the exception was created, a so-called _stack trace_. This information is stored in the `stack` property and can be helpful when trying to debug a problem: it tells us the precise function where the problem occurred and which other functions led up to the call that failed.
+In this case, we used the `Error` constructor to create our exception value. This is a standard JavaScript constructor that creates an object with a `message` property. In most JavaScript environments, instances of this constructor also gather information about the call stack that existed when the exception was created, a so-called _stack trace_. This information is stored in the `stack` property and can be helpful when trying to debug a problem: it tells us the function where the problem occurred and which functions made the failing call.
-Note that the function `look` completely ignores the possibility that `promptDirection` might go wrong. This is the big advantage of exceptions—error-handling code is necessary only at the point where the error occurs and at the point where it is handled. The functions in between can forget all about it.
+Note that the `look` function completely ignores the possibility that `promptDirection` might go wrong. This is the big advantage of exceptions: Error-handling code is necessary only at the point where the error occurs and at the point where it is handled. The functions in between can forget all about it.
Well, almost...
## Cleaning up after exceptions
-Consider the following situation: a function, `withContext`, wants to make sure that, during its execution, the top-level variable `context` holds a specific context value. After it finishes, it restores this variable to its old value.
+The effect of an exception is another kind of control flow. Every action that might cause an exception, which is pretty much every function call and property access, might cause control to suddenly leave your code.
+
+That means that when code has several side effects, even if its “regular” control flow looks like they'll always all happen, an exception might prevent some of them from taking place.
+
+Here is some really bad banking code.
```
-var context = null;
+const accounts = {
+ a: 100,
+ b: 0,
+ c: 20
+};
-function withContext(newContext, body) {
- var oldContext = context;
- context = newContext;
- var result = body();
- context = oldContext;
- return result;
+function getAccount() {
+ let accountName = prompt("Enter an account name");
+ if (!accounts.hasOwnProperty(accountName)) {
+ throw new Error(`No such account: ${accountName}`);
+ }
+ return accountName;
+}
+
+function transfer(from, amount) {
+ if (accounts[from] < amount) return;
+ accounts[from] -= amount;
+ accounts[getAccount()] += amount;
}
```
-What if `body` raises an exception? In that case, the call to `withContext` will be thrown off the stack by the exception, and `context` will never be set back to its old value.
+The `transfer` function transfers a sum of money from a given account to another, asking for the name of the other account in the process. If given an invalid account name, `getAccount` throws an exception.
+
+But `transfer` _first_ removes the money from the account, and _then_ calls `getAccount` before it adds it to another account. If it is broken off by an exception at that point, it'll just make the money disappear.
+
+That code could have been written a little more intelligently, for example by calling `getAccount` before it starts moving money around. But often problems like this occur in more subtle ways. Even functions that don't look like they will throw an exception might do so in exceptional circumstances or when they contain a programmer mistake.
-There is one more feature that `try` statements have. They may be followed by a `finally` block either instead of or in addition to a `catch` block. A `finally` block means “No matter _what_ happens, run this code after trying to run the code in the `try` block”. If a function has to clean something up, the cleanup code should usually be put into a `finally` block.
+One way to address this is to use fewer side effects. Again, a programming style that computes new values instead of changing existing data helps. If a piece of code stops running in the middle of creating a new value, no one ever sees the half-finished value, and there is no problem.
+
+But that isn't always practical. So there is another feature that `try` statements have. They may be followed by a `finally` block either instead of or in addition to a `catch` block. A `finally` block says “no matter _what_ happens, run this code after trying to run the code in the `try` block.”
```
-function withContext(newContext, body) {
- var oldContext = context;
- context = newContext;
+function transfer(from, amount) {
+ if (accounts[from] < amount) return;
+ let progress = 0;
try {
- return body();
+ accounts[from] -= amount;
+ progress = 1;
+ accounts[getAccount()] += amount;
+ progress = 2;
} finally {
- context = oldContext;
+ if (progress == 1) {
+ accounts[from] += amount;
+ }
}
}
```
-Note that we no longer have to store the result of `body` (which we want to return) in a variable. Even if we return directly from the `try` block, the `finally` block will be run. Now we can do this and be safe:
-
-```
-try {
- withContext(5, function() {
- if (context < 10)
- throw new Error("Not enough context!");
- });
-} catch (e) {
- console.log("Ignoring: " + e);
-}
-// → Ignoring: Error: Not enough context!
+This version of the function tracks its progress, and if, when leaving, it notices that it was aborted at a point where it had created an inconsistent program state, it repairs the damage it did.
-console.log(context);
-// → null
-```
+Note that, even though the `finally` code is run when an exception leaves the `try` block, it does not interfere with the exception. After the `finally` block runs, the stack continues unwinding.
-Even though the function called from `withContext` exploded, `withContext` itself still properly cleaned up the `context` variable.
+Writing programs that operate reliably even when exceptions pop up in unexpected places is very hard. Many people simply don't bother, and because exceptions are typically reserved for exceptional circumstances, the problem may occur so rarely that it is never even noticed. Whether that is a good thing or a really bad thing depends on how much damage the software will do when it fails.
## Selective catching
-When an exception makes it all the way to the bottom of the stack without being caught, it gets handled by the environment. What this means differs between environments. In browsers, a description of the error typically gets written to the JavaScript console (reachable through the browser's Tools or Developer menu).
+When an exception makes it all the way to the bottom of the stack without being caught, it gets handled by the environment. What this means differs between environments. In browsers, a description of the error typically gets written to the JavaScript console (reachable through the browser's Tools or Developer menu). Node.js, the browserless JavaScript environment we will discuss in [Chapter 20](20_node.html), is more careful about data corruption. It aborts the whole process when an unhandled exception occurs.
-For programmer mistakes or problems that the program cannot possibly handle, just letting the error go through is often okay. An unhandled exception is a reasonable way to signal a broken program, and the JavaScript console will, on modern browsers, provide you with some information about which function calls were on the stack when the problem occurred.
+For programmer mistakes, just letting the error go through is often the best you can do. An unhandled exception is a reasonable way to signal a broken program, and the JavaScript console will, on modern browsers, provide you with some information about which function calls were on the stack when the problem occurred.
-For problems that are _expected_ to happen during routine use, crashing with an unhandled exception is not a very friendly response.
+For problems that are _expected_ to happen during routine use, crashing with an unhandled exception is a terrible strategy.
-Invalid uses of the language, such as referencing a nonexistent variable, looking up a property on `null`, or calling something that's not a function, will also result in exceptions being raised. Such exceptions can be caught just like your own exceptions.
+Invalid uses of the language, such as referencing a nonexistent binding, looking up a property on `null`, or calling something that's not a function, will also result in exceptions being raised. Such exceptions can also be caught.
When a `catch` body is entered, all we know is that _something_ in our `try` body caused an exception. But we don't know _what_, or _which_ exception it caused.
-JavaScript (in a rather glaring omission) doesn't provide direct support for selectively catching exceptions: either you catch them all or you don't catch any. This makes it very easy to _assume_ that the exception you get is the one you were thinking about when you wrote the `catch` block.
+JavaScript (in a rather glaring omission) doesn't provide direct support for selectively catching exceptions: either you catch them all or you don't catch any. This makes it tempting to _assume_ that the exception you get is the one you were thinking about when you wrote the `catch` block.
-But it might not be. Some other assumption might be violated, or you might have introduced a bug somewhere that is causing an exception. Here is an example, which _attempts_ to keep on calling `promptDirection` until it gets a valid answer:
+But it might not be. Some other assumption might be violated, or you might have introduced a bug that is causing an exception. Here is an example that _attempts_ to keep on calling `promptDirection` until it gets a valid answer:
```
for (;;) {
try {
- var dir = promtDirection("Where?"); // ← typo!
+ let dir = promtDirection("Where?"); // ← typo!
console.log("You chose ", dir);
break;
} catch (e) {
@@ -300,108 +333,93 @@ for (;;) {
}
```
-The `for (;;)` construct is a way to intentionally create a loop that doesn't terminate on its own. We break out of the loop only when a valid direction is given. _But_ we misspelled `promptDirection`, which will result in an “undefined variable” error. Because the `catch` block completely ignores its exception value (`e`), assuming it knows what the problem is, it wrongly treats the variable error as indicating bad input. Not only does this cause an infinite loop, but it also “buries” the useful error message about the misspelled variable.
+The `for (;;)` construct is a way to intentionally create a loop that doesn't terminate on its own. We break out of the loop only when a valid direction is given. _But_ we misspelled `promptDirection`, which will result in an “undefined variable” error. Because the `catch` block completely ignores its exception value (`e`), assuming it knows what the problem is, it wrongly treats the binding error as indicating bad input. Not only does this cause an infinite loop, it also “buries” the useful error message about the misspelled binding.
As a general rule, don't blanket-catch exceptions unless it is for the purpose of “routing” them somewhere—for example, over the network to tell another system that our program crashed. And even then, think carefully about how you might be hiding information.
-So we want to catch a _specific_ kind of exception. We can do this by checking in the `catch` block whether the exception we got is the one we are interested in and by rethrowing it otherwise. But how do we recognize an exception?
+So we want to catch a _specific_ kind of exception. We can do this by checking in the `catch` block whether the exception we got is the one we are interested in and rethrowing it otherwise. But how do we recognize an exception?
-Of course, we could match its `message` property against the error message we happen to expect. But that's a shaky way to write code—we'd be using information that's intended for human consumption (the message) to make a programmatic decision. As soon as someone changes (or translates) the message, the code will stop working.
+We could compare its `message` property against the error message we happen to expect. But that's a shaky way to write code—we'd be using information that's intended for human consumption (the message) to make a programmatic decision. As soon as someone changes (or translates) the message, the code will stop working.
Rather, let's define a new type of error and use `instanceof` to identify it.
```
-function InputError(message) {
- this.message = message;
- this.stack = (new Error()).stack;
-}
-InputError.prototype = Object.create(Error.prototype);
-InputError.prototype.name = "InputError";
-```
-
-The prototype is made to derive from `Error.prototype` so that `instanceof Error` will also return true for `InputError` objects. It's also given a `name` property since the standard error types (`Error`, `SyntaxError`, `ReferenceError`, and so on) also have such a property.
-
-The assignment to the `stack` property tries to give this object a somewhat useful stack trace, on platforms that support it, by creating a regular error object and then using that object's `stack` property as its own.
-
-Now `promptDirection` can throw such an error.
+class InputError extends Error {}
-```
function promptDirection(question) {
- var result = prompt(question, "");
+ let result = prompt(question);
if (result.toLowerCase() == "left") return "L";
if (result.toLowerCase() == "right") return "R";
throw new InputError("Invalid direction: " + result);
}
```
-And the loop can catch it more carefully.
+The new error class extends `Error`. It doesn't define its own constructor, which means that it inherits the `Error` constructor, which expects a string message as argument. In fact, it doesn't define anything at all—the class is empty. `InputError` objects behave like `Error` objects, except that they have a different class by which we can recognize them.
+
+Now the loop can catch these more carefully.
```
for (;;) {
try {
- var dir = promptDirection("Where?");
+ let dir = promptDirection("Where?");
console.log("You chose ", dir);
break;
} catch (e) {
- if (e instanceof InputError)
+ if (e instanceof InputError) {
console.log("Not a valid direction. Try again.");
- else
+ } else {
throw e;
+ }
}
}
```
-This will catch only instances of `InputError` and let unrelated exceptions through. If you reintroduce the typo, the undefined variable error will be properly reported.
+This will catch only instances of `InputError` and let unrelated exceptions through. If you reintroduce the typo, the undefined binding error will be properly reported.
## Assertions
-_Assertions_ are a tool to do basic sanity checking for programmer errors. Consider this helper function, `assert`:
+_Assertions_ are checks inside a program that verify that something is the way it is supposed to be. They are used not to handle situations that can come up in normal operation, but to find programmer mistakes.
-```
-function AssertionFailed(message) {
- this.message = message;
-}
-AssertionFailed.prototype = Object.create(Error.prototype);
+If, for example, `firstElement` is described as a function that should never be called on empty arrays, we might write it like this:
-function assert(test, message) {
- if (!test)
- throw new AssertionFailed(message);
-}
-
-function lastElement(array) {
- assert(array.length > 0, "empty array in lastElement");
- return array[array.length - 1];
+```
+function firstElement(array) {
+ if (array.length == 0) {
+ throw new Error("firstElement called with []");
+ }
+ return array[0];
}
```
-This provides a compact way to enforce expectations, helpfully blowing up the program if the stated condition does not hold. For instance, the `lastElement` function, which fetches the last element from an array, would return `undefined` on empty arrays if the assertion was omitted. Fetching the last element from an empty array does not make much sense, so it is almost certainly a programmer error to do so.
+Now, instead of silently returning undefined (which you get when reading an array property that does not exist), this will loudly blow up your program as soon as you misuse it. This makes it less likely for such mistakes to go unnoticed, and easier to find their cause when they occur.
-Assertions are a way to make sure mistakes cause failures at the point of the mistake, rather than silently producing nonsense values that may go on to cause trouble in an unrelated part of the system.
+I do not recommend trying to write assertions for every possible kind of bad input. That'd be a lot of work and would lead to very noisy code. You'll want to reserve them for mistakes that are easy to make (or that you find yourself making).
## Summary
-Mistakes and bad input are facts of life. Bugs in programs need to be found and fixed. They can become easier to notice by having automated test suites and adding assertions to your programs.
+Mistakes and bad input are facts of life. An important part of programming is finding, diagnosing, and fixing bugs. Problems can become easier to notice if you have an automated test suite or add assertions to your programs.
-Problems caused by factors outside the program's control should usually be handled gracefully. Sometimes, when the problem can be handled locally, special return values are a sane way to track them. Otherwise, exceptions are preferable.
+Problems caused by factors outside the program's control should usually be handled gracefully. Sometimes, when the problem can be handled locally, special return values are a good way to track them. Otherwise, exceptions may be preferable.
-Throwing an exception causes the call stack to be unwound until the next enclosing `try/catch` block or until the bottom of the stack. The exception value will be given to the `catch` block that catches it, which should verify that it is actually the expected kind of exception and then do something with it. To deal with the unpredictable control flow caused by exceptions, `finally` blocks can be used to ensure a piece of code is _always_ run when a block finishes.
+Throwing an exception causes the call stack to be unwound until the next enclosing `try/catch` block or until the bottom of the stack. The exception value will be given to the `catch` block that catches it, which should verify that it is actually the expected kind of exception and then do something with it. To help address the unpredictable control flow caused by exceptions, `finally` blocks can be used to ensure that a piece of code _always_ runs when a block finishes.
## Exercises
### Retry
-Say you have a function `primitiveMultiply` that, in 50 percent of cases, multiplies two numbers, and in the other 50 percent, raises an exception of type `MultiplicatorUnitFailure`. Write a function that wraps this clunky function and just keeps trying until a call succeeds, after which it returns the result.
+Say you have a function `primitiveMultiply` that, in 20 percent of cases, multiplies two numbers, and in the other 80 percent, raises an exception of type `MultiplicatorUnitFailure`. Write a function that wraps this clunky function and just keeps trying until a call succeeds, after which it returns the result.
Make sure you handle only the exceptions you are trying to handle.
```
-function MultiplicatorUnitFailure() {}
+class MultiplicatorUnitFailure extends Error {}
function primitiveMultiply(a, b) {
- if (Math.random() < 0.5)
+ if (Math.random() < 0.2) {
return a * b;
- else
- throw new MultiplicatorUnitFailure();
+ } else {
+ throw new MultiplicatorUnitFailure("Klunk");
+ }
}
function reliableMultiply(a, b) {
@@ -412,7 +430,7 @@ console.log(reliableMultiply(8, 8));
// → 64
```
-The call to `primitiveMultiply` should obviously happen in a `try` block. The corresponding `catch` block should rethrow the exception when it is not an instance of `MultiplicatorUnitFailure` and ensure the call is retried when it is.
+The call to `primitiveMultiply` should definitely happen in a `try` block. The corresponding `catch` block should rethrow the exception when it is not an instance of `MultiplicatorUnitFailure` and ensure the call is retried when it is.
To do the retrying, you can either use a loop that breaks only when a call succeeds—as in the [`look` example](08_error.html#look) earlier in this chapter—or use recursion and hope you don't get a string of failures so long that it overflows the stack (which is a pretty safe bet).
@@ -421,10 +439,10 @@ To do the retrying, you can either use a loop that breaks only when a call succe
Consider the following (rather contrived) object:
```
-var box = {
+const box = {
locked: true,
- unlock: function() { this.locked = false; },
- lock: function() { this.locked = true; },
+ unlock() { this.locked = false; },
+ lock() { this.locked = true; },
_content: [],
get content() {
if (this.locked) throw new Error("Locked!");
@@ -433,11 +451,22 @@ var box = {
};
```
-It is a box with a lock. Inside is an array, but you can get at it only when the box is unlocked. Directly accessing the `_content` property is not allowed.
+It is a box with a lock. There is an array in the box, but you can get at it only when the box is unlocked. Directly accessing the private `_content` property is forbidden.
Write a function called `withBoxUnlocked` that takes a function value as argument, unlocks the box, runs the function, and then ensures that the box is locked again before returning, regardless of whether the argument function returned normally or threw an exception.
```
+const box = {
+ locked: true,
+ unlock() { this.locked = false; },
+ lock() { this.locked = true; },
+ _content: [],
+ get content() {
+ if (this.locked) throw new Error("Locked!");
+ return this._content;
+ }
+};
+
function withBoxUnlocked(body) {
// Your code here.
}
@@ -459,6 +488,6 @@ console.log(box.locked);
For extra points, make sure that if you call `withBoxUnlocked` when the box is already unlocked, the box stays unlocked.
-This exercise calls for a `finally` block, as you probably guessed. Your function should first unlock the box and then call the argument function from inside a `try` body. The `finally` block after it should lock the box again.
+This exercise calls for a `finally` block. Your function should first unlock the box and then call the argument function from inside a `try` body. The `finally` block after it should lock the box again.
To make sure we don't lock the box when it wasn't already locked, check its lock at the start of the function and unlock and lock it only when it started out locked.