Mastering error handling and debugging is crucial for developers to build reliable applications. This chapter covers try-catch statements, error types, debugging with consoles, and browser debugging tools.
Error Handling and Debugging
Topic: Try-Catch Statements
What does the try block in a try...catch statement do?
A) Executes code and logs errors automatically
B) Defines a block of code to be tested for errors
C) Suppresses all errors in the code
D) Executes only if no errors occur
Which block handles the error in a try...catch statement?
A) Try
B) Catch
C) Finally
D) Error
What happens if an error occurs in the try block?
A) The error is ignored
B) The program stops executing
C) The catch block is executed
D) The finally block executes
Which of the following is a valid syntax for try...catch in JavaScript?
A) try { code } catch { error }
B) try { code } catch (error) { handleError }
C) try (code) catch { error }
D) try { code } error (catch)
What is the purpose of the finally block in try...catch?
A) To execute if no error occurs
B) To clean up resources regardless of errors
C) To suppress errors
D) To log errors only
Can a try...catch statement have only a try block?
A) Yes, it is mandatory
B) No, a catch or finally block is required
C) Yes, but only in strict mode
D) No, it requires an error object
In a try...catch statement, what type of errors does the catch block handle?
A) Logical errors
B) Syntax errors
C) Runtime errors
D) All errors
What will happen if there is an error in the catch block?
A) It is ignored silently
B) It stops program execution
C) It triggers another error
D) It is caught by the browser’s error handler
Which keyword is used to manually throw an error in JavaScript?
A) throw
B) raise
C) error
D) halt
What should you do if you want to log a custom error message inside a catch block?