0
0
Javascriptprogramming~5 mins

Catching runtime errors in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of try...catch in JavaScript?
It is used to handle runtime errors gracefully by trying a block of code and catching errors if they occur, preventing the program from crashing.
Click to reveal answer
beginner
What does the catch block receive as its parameter?
The catch block receives the error object that was thrown in the try block, which contains information about the error.
Click to reveal answer
intermediate
How can you throw a custom error in JavaScript?
You can use the throw statement followed by an error object or message, for example: throw new Error('Custom message').
Click to reveal answer
beginner
What happens if an error occurs outside a try...catch block?
The error will be unhandled and usually causes the program to stop or crash, showing an error message in the console.
Click to reveal answer
intermediate
Can you use try...catch to handle asynchronous errors directly?
No, try...catch works for synchronous code. For asynchronous code, you handle errors with catch on promises or use async/await with try...catch.
Click to reveal answer
What keyword starts a block of code where you expect an error might happen?
Atry
Bcatch
Cthrow
Derror
Which block runs only if an error occurs in the try block?
Afinally
Bcatch
Cthrow
Derror
How do you create a new error to throw?
Atry Error('message')
Bthrow Error('message')
Ccatch Error('message')
Dnew Error('message')
What happens if you don't catch an error?
AThe program crashes or stops
BThe program continues silently
CThe error is ignored
DThe error is fixed automatically
Which is the correct way to catch errors from a promise?
Athrow promise
Btry { promise } catch(error) {}
Cpromise.catch(error => ...)
Dcatch(promise)
Explain how try...catch helps in handling runtime errors in JavaScript.
Think about what happens when code inside try fails.
You got /4 concepts.
    Describe how you would handle errors in asynchronous JavaScript code.
    Remember that async code needs special handling.
    You got /4 concepts.