Errors and exceptions
1.[3p] What does a stack trace tell you?
What does a stack trace tell you?
The answer is: Where the program stopped and the chain of calls that led there, which is not necessarily where the mistake is
The answer is: Where the program stopped and the chain of calls that led there, which is not necessarily where the mistake is
The answer is: Where the program stopped and the chain of calls that led there, which is not necessarily where the mistake is
2.[3p] Match each error kind to what causes it.
Match each error kind to what causes it.
ReferenceError
TypeError
RangeError
SyntaxError
text that is not a valid program, including malformed JSON
a name that does not exist, usually a typo
a value outside an allowed range, or a recursion with no reachable base case
a value that is not the kind the operation needs
Show the answer
ReferenceError: a name that does not exist, usually a typo TypeError: a value that is not the kind the operation needs RangeError: a value outside an allowed range, or a recursion with no reachable base case SyntaxError: text that is not a valid program, including malformed JSON
3.[3p] Put the frames of a stack trace in the order they are printed, from top to bottom.
Put the frames of a stack trace in the order they are printed, from top to bottom.
The function that called the one that failed
The function where execution stopped
The top level line that started everything
Show the answer
c, a, b
4.[3p] Why is catch (err) { } with an empty body worse than not catching at all?
Why is catch (err) { } with an empty body worse than not catching at all?
The answer is: A crash names the failure, points at a line and stops before the damage spreads, and swallowing it destroys all three
The answer is: A crash names the failure, points at a line and stops before the damage spreads, and swallowing it destroys all three
The answer is: A crash names the failure, points at a line and stops before the damage spreads, and swallowing it destroys all three
5.[2p] finally runs even when the try block exits through a return.
finally runs even when the try block exits through a return.
The answer is: True
6.[2p] throw "oops" is legal. Why throw new Error("oops") instead?
throw "oops" is legal. Why throw new Error("oops") instead?
The answer is: An Error object carries a name, a message and a stack, and a bare string carries none of them
The answer is: An Error object carries a name, a message and a stack, and a bare string carries none of them
The answer is: An Error object carries a name, a message and a stack, and a bare string carries none of them
7.[3p] Which of these are reasons to validate input at the edge of a program rather than in every function?
Which of these are reasons to validate input at the edge of a program rather than in every function?
Select all that apply
The answer is: The checks that remain are few enough to be worth reading, The failure is reported at the point the bad data arrived, Values inside the program can then be trusted
8.[2p] Which grade of wrongness produces no message of any kind? Write one word.
Which grade of wrongness produces no message of any kind? Write one word.
9.[2p] A syntax error is reported at line 60, but the file looks correct there. Where is the mistake most likely to be?
A syntax error is reported at line 60, but the file looks correct there. Where is the mistake most likely to be?
The answer is: Above line 60, where an unclosed brace or quote left everything after it parseable
The answer is: Above line 60, where an unclosed brace or quote left everything after it parseable
The answer is: Above line 60, where an unclosed brace or quote left everything after it parseable