What is an Error in JavaScript?
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.
Name the three most common built-in Error Types.
SyntaxError: Violation of JavaScript’s grammar rules.
TypeError: Incorrect data type used in an operation.
ReferenceError: Using an undeclared variable.
What are some other built-in Error types in JavaScript?
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).
Why use try…catch blocks?
To catch errors and prevent crashes.
To handle errors gracefully (e.g., provide fallback logic).
To isolate potentially problematic code.
To improve application reliability.
Describe the try block.
Contains code that might throw an error.
If an error occurs, execution jumps to the catch block.
Describe the catch block.
Contains code that handles an error thrown in the try block.
Receives the error object as an argument.
What is the purpose of the finally block in a try…catch statement?
Contains code that always executes, regardless of whether an error occurred or not.
Used for cleanup tasks (e.g., closing files, releasing resources).
Describe the Error Object.
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).
Explain what a ‘call stack’ is in the context of error handling.
A record of functions called leading up to an error.
Used to trace and debug the source of the error.
What does the term ‘throw an error’ mean in JavaScript?
Intentionally create and raise an error using the throw keyword.
JavaScript engine searches for a try…catch block to handle it.
Besides error handling, what’s another use of throwing errors?
To report validation errors.
To stop execution when encountering unresolvable issues.
What is an ‘unhandled rejection’?
Occurs when a Promise is rejected and no .catch() handler is attached.
Browsers and Node.js report unhandled rejections.
What is the primary purpose of the try…catch statement in JavaScript?
To handle errors gracefully, prevent application crashes, and provide safe code execution.
What code goes inside the try block?
Code that you suspect might throw an error.
What happens if an error occurs inside the try block?
Execution stops immediately and control jumps to the catch block.
What code goes inside the catch block?
Code that handles the error, logs it, or provides a fallback.
What is the purpose of the error parameter in the catch block?
It holds the error object with details like name, message, and stack.
What are the typical properties of an error object?
name: Type of error
message: Description of error
stack: Call stack trace (optional)
What happens after the catch block finishes executing?
Unless interrupted (return/throw), execution continues after the try…catch.
What is the purpose of the finally block, and when is it executed?
Always executes, regardless of errors.
Used for cleanup (closing files, etc.).
Give an example of a common use case for the finally block.
Closing a file, releasing a DB connection, or cleaning up resources.
Why is try…catch useful when dealing with user input?
User input is unpredictable and may cause errors.
Why is try…catch important when making API calls?
API calls may fail due to network/server issues.
try…catch lets you handle failures.
What is ‘fallback logic’ and how is it used in try…catch?
Alternative actions when primary code fails.
Implemented in the catch block.