JavaScript Error Handling (try, catch, finally) Quiz
Tackle 40 questions on why try/catch exists, how to structure catch/finally blocks, inspecting error objects, throwing custom errors, handling specific cases, nesting handlers, and modern best practices.
Question 1
What is the primary reason to use try/catch?
Question 2
Which block executes only when an exception is thrown inside try?
Question 3
Which statement about finally is correct?
Question 4
Can a try block exist without a catch block?
Question 5
Where do thrown errors propagate by default?
Question 6
What values can be thrown in JavaScript?
Question 7
What parameter does catch receive?
Question 8
Why is try/catch considered expensive when overused?
Question 9
What does this snippet log?
try {
throw new Error('fail')
} catch (err) {
console.log(err.message)
}Question 10
How does catch behave here?
try {
JSON.parse('{ invalid }')
} catch (error) {
console.error('Parsing failed', error.name)
}Question 11
What information does err.stack typically contain?
Question 12
How do you throw a custom Error subclass?
Question 13
What does this custom error output?
class ValidationError extends Error {
constructor(msg) {
super(msg)
this.name = 'ValidationError'
}
}
try {
throw new ValidationError('Missing email')
} catch (err) {
console.log(err.name, err.message)
}Question 14
How can catch handle different error types?
Question 15
Why is throwing strings discouraged?
Question 16
What does catch { ... } without a parameter do?
Question 17
Why should you avoid swallowing errors silently?
Question 18
What does this nested handler log?
try {
try {
throw new Error('inner')
} catch (err) {
console.log('Inner handled', err.message)
throw err
}
} catch (outerErr) {
console.log('Outer handled', outerErr.message)
}Question 19
How does this code handle multiple error types?
try {
risky()
} catch (err) {
if (err instanceof SyntaxError) {
handleSyntax(err)
} else if (err instanceof TypeError) {
handleType(err)
} else {
throw err
}
}Question 20
When should you rethrow an error after logging?
Question 21
What does this helper demonstrate?
function safe(fn) {
try {
return { ok: true, value: fn() }
} catch (error) {
return { ok: false, error }
}
}
const result = safe(() => JSON.parse(data))Question 22
Why throw custom errors for validation failures?
Question 23
How can you attach metadata to errors before rethrowing?
try {
doTask()
} catch (err) {
err.taskId = currentTaskId
throw err
}Question 24
What does this parsing helper return on failure?
function parse(json) {
try {
return JSON.parse(json)
} catch (err) {
return null
}
}
console.log(parse('oops'))Question 25
Why should catch blocks include logging or metrics?
Question 26
What does this try/finally wrapper guarantee?
let lock = acquire()
try {
useLock(lock)
} finally {
lock.release()
}Question 27
What is the effect of returning inside finally?
Question 28
How does this async try/catch handle promise rejections?
async function loadData() {
try {
const data = await fetchJSON()
return data
} catch (error) {
console.error('Fetch failed', error)
return null
}
}Question 29
Why is finally recommended for cleanup?
Question 30
Why should asynchronous cleanup inside finally be awaited?
Question 31
How do nested try/catch blocks aid recovery?
try {
handleRequest()
} catch (err) {
try {
recover(err)
} catch (recoverErr) {
logCritical(recoverErr)
throw recoverErr
}
}Question 32
How can you avoid duplicate cleanup logic in nested handlers?
Question 33
What is the fail-fast principle?
Question 34
Why centralize error handling in large apps?
window.onerror = (msg, url, line, col, error) => {
reportToService({ msg, url, line, col, stack: error?.stack })
}
process.on('uncaughtException', err => {
console.error('Fatal', err)
notifyOps(err)
})Question 35
What should a catch block do when it cannot recover?
Question 36
What best practice applies when wrapping third-party calls?
Question 37
Why should logs include contextual data such as user actions or identifiers?
Question 38
How does try/catch interact with asynchronous callbacks (not using async/await)?
Question 39
Why convert user-facing errors into friendly messages?
Question 40
Which statement summarizes robust error handling?
