Error Handling with try/catch
JavaScript uses try...catch
to handle errors gracefully.
try {
// code that may throw
let result = riskyOperation();
} catch (error) {
console.error("Something went wrong:", error);
} finally {
console.log("Always runs");
}
finally
is optional and runs whether an error occurred or not.