Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
βΆ Do the exercisesExercise 1 β Quiz: Nothing Ran
A program prints nothing at all, not even its first line. Which kind of error is it?
Hint : One kind stops the program before the first instruction.
Exercise 2 β Quiz: Crashed Halfway
A program prints three lines, then stops with red text. Which kind of error is it?
Hint : Something ran before the crash.
Exercise 3 β Quiz: Wrong but Silent
A program runs to the end with no message, but the final score is wrong. Which kind of error is it?
Hint : No red text does not mean correct code.
Exercise 4 β Quiz: What Is undefined?
The message says `Cannot read properties of undefined (reading 'hp')`. What is `undefined`?
console.log(team[4].hp);team[4]hpteamconsoleHint : Look at what sits right before the property named in the message.
Exercise 5 β Quiz: Not Defined
You see `levl is not defined`. What is the most likely cause?
Hint : The message names something that was searched for and never found.
Exercise 6 β Quiz: TypeError
Which error type does this produce?
const n = 5;
n();TypeErrorReferenceErrorRangeErrorSyntaxErrorHint : The variable exists but is used as something it isn't.
Exercise 7 β Quiz: ReferenceError
Which error type does this produce?
console.log(missingName);ReferenceErrorTypeErrorRangeErrorSyntaxErrorHint : The name was never declared anywhere.
Exercise 8 β Quiz: RangeError
Which error type does this produce?
"#".repeat(-5);RangeErrorTypeErrorReferenceErrorSyntaxErrorHint : The number is valid, just outside what the method accepts.
Exercise 9 β Quiz: SyntaxError
Which line is a syntax error?
const x = ;const x = y;const x = null.z;const x = [].pop();Hint : One line cannot even be read.
Exercise 10 β Quiz: Clamp It
Stop the count from going negative before drawing.
Hint : Catch the value when it drops too low and bring it back to the minimum.
Exercise 11 β Quiz: try Then Crash
What does this code print?
try {
console.log("A");
undefined.x;
console.log("B");
} catch (e) {
console.log("C");
}
console.log("D");A, C, DA, B, C, DA, DC, DHint : A crash skips the rest of try and jumps to catch.
Exercise 12 β Quiz: try With No Crash
What does this code print?
try {
console.log("A");
} catch (e) {
console.log("B");
}
console.log("C");A, CA, B, CB, CA onlyHint : catch only runs when something went wrong.
Exercise 13 β Quiz: Protect It
Wrap the risky line so the program survives.
Hint : One keyword opens the protected area, the other names what runs on failure.
Exercise 14 β Quiz: The Error Object
Inside `catch (error)`, which property holds the type of the error?
error.nameerror.messageerror.typeerror.kindHint : One property is the family, the other the explanation.
Exercise 15 β Quiz: finally After a Crash
What does this code print?
try {
undefined.x;
} catch (e) {
console.log("B");
} finally {
console.log("C");
}B, CB onlyC onlyHint : Both remaining blocks run after a crash.
Exercise 16 β Quiz: finally Without a Crash
What does this code print?
try {
console.log("A");
} catch (e) {
console.log("B");
} finally {
console.log("C");
}A, CA, B, CA onlyC onlyHint : One block is skipped when all goes well, the other never is.
Exercise 17 β Quiz: finally vs return
What does this code print?
function f() {
try {
return "done";
} finally {
console.log("cleanup");
}
}
console.log(f());cleanup then donedone then cleanupdone onlycleanup onlyHint : The cleanup runs on the way out, before the value reaches the caller.
Exercise 18 β Quiz: Always Close
Make the closing line run whether the write works or not.
Hint : One keyword introduces the block that always runs, and the slot must end up shut.
Exercise 19 β Quiz: throw Caught
What does this code print?
function f() {
throw new Error("boom");
}
try {
f();
} catch (e) {
console.log(e.message);
}boomErrorundefinedHint : The thrown message travels to the nearest catch.
Exercise 20 β Quiz: After a throw
What does this code print?
function f() {
console.log("A");
throw new Error("x");
console.log("B");
}
try {
f();
} catch (e) {
console.log("C");
}A, CA, B, CA onlyC onlyHint : A throw ends the function on the spot.
Exercise 21 β Quiz: Raise the Alarm
Refuse a negative amount with an error.
Hint : One keyword launches the alarm, and the built-in object carrying a message has a short name.
Exercise 22 β Quiz: Uncaught
A `throw` happens and no `catch` exists anywhere. What happens?
Hint : An error keeps travelling up until something catches it, or nothing does.
Exercise 23 β Quiz: Where to Validate
Where is the best place to check inputs and throw?
Hint : Refuse bad data before doing any real work with it.
Exercise 24 β Quiz: Small try Blocks
Why keep a `try` block small?
Hint : Think about what happens to the lines after a crash inside try.
Exercise 25 β Quiz: Reading Order
What is the first thing to read in an error message?
Hint : The first word of the message tells you which kind of problem it is.