Start here.
A program can meet a situation it cannot handle normally: a value may have the wrong shape, a file may be unavailable, or a request may fail. JavaScript represents many such failures with Error objects. throw stops the current normal flow and sends an error outward through callers until a matching catch handles it. If nothing catches it, the environment reports the failure. A thrown error is different from a function returning an ordinary value such as null.
The try block contains code that may throw; catch receives the error and can decide what to do; finally runs afterward whether the operation succeeded or failed. Catch narrowly when you can add useful context or choose a meaningful recovery. Silently returning a guessed value can hide a real problem. Error messages should explain what went wrong, while the program's recovery should match what the user can still do.
A syntax error can stop code before it begins; a runtime error occurs while executing. Neither is a personal failure. They are signals about an assumption the code made. Read the message and the location, then trace the value involved: was it undefined, the wrong type, or a rejected operation? That reasoning is part of understanding code, even when this course presents the outcome for you.
Build the model.
An exception interrupts the current path until a matching catch handles it. This makes failure visible across several calls, but catching everything too early can hide the reason something went wrong. Not every unexpected value is an exception; sometimes a clear return value is the better contract.
Use try and catch around the work that can actually fail, then recover or explain the failure there. finally runs during normal completion and error paths, making it useful for cleanup. Avoid replacing a detailed failure with a vague message before it reaches a place that can act on it.
How it works.
A throw abandons the current path and searches outward for a catch. Calls between the throw and catch do not resume normally. That makes an exception appropriate when a function cannot produce the result it promised, such as JSON.parse receiving malformed text. A catch should decide what the caller or user can do next, rather than silently pretending the operation succeeded.
Keep the catch narrow enough to know what may have failed. If the block contains unrelated operations, a single catch may mislabel a network failure as an input problem. finally runs when leaving try or catch and is useful for releasing resources or restoring state. In asynchronous code, await a promise inside try if you want that catch to handle its rejection.
Read it line by line.
const input = '{bad json';
try {
console.log(JSON.parse(input));
} catch (error) {
console.log('The text is not valid JSON');
}First notice. The catch handles a particular boundary: text supplied from outside the program.
input holds text that is not valid JSON. JSON.parse(input) tries to turn it into a JavaScript value and throws a SyntaxError. The console.log around that call never runs, because the throw interrupts the expression. Execution jumps to catch, which displays the explanatory message. With valid JSON, the parsed value would be displayed and catch would be skipped.
See the rule again.
function readCount(text) {
try { return JSON.parse(text).count; }
catch { return 0; }
}
console.log(readCount('{bad json'));The output is 0 because parsing throws and the catch returns a fallback. This fallback is a product decision, not a universal rule: if malformed data must be reported, return a structured error or let the exception reach a caller that can explain it.
One thing to remember.
An error should travel to the part of the program that can respond meaningfully.