Exceptions Flashcards

1
Q

What is an exception?

A

An exception is an event that occurs during program execution to indicate an anomalous or exceptional condition. In JavaScript, a typical exception is an object of theErrortype or an object that inherits from theErrortype

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

are all errors exceptions?

A

No, Not all errors are exceptions. For instance, your program may display an error message when the user enters some invalid information. However, it probably won’t throw an exception when it does. Instead, it will validate the inputs to see whether there is a problem. If there is, it will display an error message and ask the user to try again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What type do we use to throw an error?

A

Most JavaScript code throws anew Errorto raise an exception rather than one of the 7 built-in types that inherit fromError.

class DivideByZeroError extends Error {}

function div(first, second) {
if (second === 0) {
throw new DivideByZeroError(“Divide by zero!”);
}

return first / second;
}

let result = div(1, 0); // DivideByZeroError: Divide by zero!
console.log(result); // not run

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we handle an exception?

A

class DivideByZeroError extends Error {}

function div(first, second) {
if (second === 0) {
throw new DivideByZeroError(“Divide by zero!”);
}

return first / second;
}

function divideOneBy(divisor) {
try {
let result = div(1, divisor);
console.log(result);
} catch (error) {
if (error instanceof DivideByZeroError) {
console.log(${error.message} ignored);
} else {
throw error;
}
}
}

divideOneBy(1); // 1
divideOneBy(0); // Divide by zero! ignored

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does a try/catch work?

A

When a JavaScript program throws an exception, the exception bubbles up through the program looking for atryblock that contains the code that eventually led to the exception. If the exception does encounter atryblock, it then exits from thetryblock without executing any remaining code and executes thecatchblock. If a re-throw does not occur in thecatchblock and thecatchblock does not throw an exception of its own, the program discards the exception object and resumes execution with the code after thetry/catchstatement. If thecatchblock does raise a new exception object it again begins to bubble up through the program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When should you throw and catch?

A

You should throw an exception when an event occurs that should not be ignored or when the condition is truly anomalous or exceptional. You should not throw an exception if you can handle the problem in your local code, or if it’s a normal or expected part of the control flow.

for example, requesting code from an external server is not something you can control:

let data = IO.fileRead(file); // returns null on error

if (data === null) {
throw new Error(Can't open required file ${file}!);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly