js_all_error_flashcards

... (45 cards)

1
Q

What is an Error in JavaScript?

A

An error indicates an unexpected or exceptional event during code execution.
It disrupts the normal program flow.
Examples: Syntax errors, type errors, invalid operations, network issues.

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

Name the three most common built-in Error Types.

A

SyntaxError: Violation of JavaScript’s grammar rules.
TypeError: Incorrect data type used in an operation.
ReferenceError: Using an undeclared variable.

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

What are some other built-in Error types in JavaScript?

A

RangeError: Number outside the allowed range.
URIError: Problem with encodeURI() or decodeURI().
EvalError: Error in the eval() function (largely deprecated).
InternalError: Internal JavaScript engine error (rare).

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

Why use try…catch blocks?

A

To catch errors and prevent crashes.
To handle errors gracefully (e.g., provide fallback logic).
To isolate potentially problematic code.
To improve application reliability.

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

Describe the try block.

A

Contains code that might throw an error.
If an error occurs, execution jumps to the catch block.

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

Describe the catch block.

A

Contains code that handles an error thrown in the try block.
Receives the error object as an argument.

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

What is the purpose of the finally block in a try…catch statement?

A

Contains code that always executes, regardless of whether an error occurred or not.
Used for cleanup tasks (e.g., closing files, releasing resources).

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

Describe the Error Object.

A

Created when an error occurs.
Properties:
name: Error type (e.g., “TypeError”, “ReferenceError”).
message: Human-readable error description.
stack: Call stack showing function sequence (not always available).

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

Explain what a ‘call stack’ is in the context of error handling.

A

A record of functions called leading up to an error.
Used to trace and debug the source of the error.

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

What does the term ‘throw an error’ mean in JavaScript?

A

Intentionally create and raise an error using the throw keyword.
JavaScript engine searches for a try…catch block to handle it.

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

Besides error handling, what’s another use of throwing errors?

A

To report validation errors.
To stop execution when encountering unresolvable issues.

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

What is an ‘unhandled rejection’?

A

Occurs when a Promise is rejected and no .catch() handler is attached.
Browsers and Node.js report unhandled rejections.

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

What is the primary purpose of the try…catch statement in JavaScript?

A

To handle errors gracefully, prevent application crashes, and provide safe code execution.

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

What code goes inside the try block?

A

Code that you suspect might throw an error.

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

What happens if an error occurs inside the try block?

A

Execution stops immediately and control jumps to the catch block.

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

What code goes inside the catch block?

A

Code that handles the error, logs it, or provides a fallback.

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

What is the purpose of the error parameter in the catch block?

A

It holds the error object with details like name, message, and stack.

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

What are the typical properties of an error object?

A

name: Type of error
message: Description of error
stack: Call stack trace (optional)

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

What happens after the catch block finishes executing?

A

Unless interrupted (return/throw), execution continues after the try…catch.

20
Q

What is the purpose of the finally block, and when is it executed?

A

Always executes, regardless of errors.
Used for cleanup (closing files, etc.).

21
Q

Give an example of a common use case for the finally block.

A

Closing a file, releasing a DB connection, or cleaning up resources.

22
Q

Why is try…catch useful when dealing with user input?

A

User input is unpredictable and may cause errors.

23
Q

Why is try…catch important when making API calls?

A

API calls may fail due to network/server issues.
try…catch lets you handle failures.

24
Q

What is ‘fallback logic’ and how is it used in try…catch?

A

Alternative actions when primary code fails.
Implemented in the catch block.

25
What is an 'Unhandled Rejection' and how does it relate to try...catch?
Error in a Promise without a .catch(). Use try...catch with async/await or .catch() for Promises.
26
Explain try...catch vs .catch for Promise handling.
try...catch handles sync and async with await. .catch handles rejections in Promises.
27
Can you nest try...catch blocks? Why?
Yes. Use for localized and broader error handling.
28
What is the purpose of re-throwing an error?
Allows higher-level error handling after partial handling or logging.
29
What is the purpose of throw in JavaScript?
Raises an error, interrupts execution, and sends control to a try...catch block.
30
Should you throw a string or an Error object? Why?
Use an Error object. Includes name, message, and stack trace. String lacks structure.
31
What is the purpose of the message property when throwing an Error?
Provides a human-readable description of the error. Useful for debugging.
32
Give examples of scenarios where throwing custom errors is useful.
Form validation, function misuse, API failure, missing files, invalid data.
33
What are best practices for custom error messages?
Be specific, informative, and user-friendly. Avoid vague messages. Include relevant data.
34
How can you create a custom error class in JavaScript?
Use `class CustomError extends Error` with a constructor that calls super(message).
35
Why is creating a custom error class useful?
Enables instanceof checks, targeted error handling, better organization.
36
How would you catch a specific custom error?
Use instanceof in the catch block to filter error types.
37
What’s the most common pitfall when working with loops?
Infinite loops. Prevent by checking conditions, using debug tools, and iteration limits.
38
Why is using for...in with arrays a bad idea?
Iterates property keys (not values), may include inherited properties. Use for or for...of.
39
What’s the problem with having an empty catch block?
Suppresses errors, making debugging harder. Always handle or log errors.
40
Why should you use meaningful error messages?
Helps identify and fix issues quickly. Generic messages are not helpful.
41
What are strategies to avoid deeply nested loops?
Break logic into functions. Use map/filter/reduce. Avoid unnecessary nesting.
42
What is 'premature optimization' in loops?
Optimizing before knowing if there's a performance issue. Focus on clarity first.
43
What is a 'loop invariant' and why is it useful?
Condition true before/during/after each iteration. Helps ensure loop correctness.
44
forEach vs for...of: What’s a key control flow difference?
You can’t use break/continue in forEach. Use for...of if you need them.
45
What is an 'off-by-one' error and how to avoid it?
Loop runs 1 time too few/many. Check < vs <= conditions. Test edge cases.