Skip to content

Latest commit

 

History

History
43 lines (31 loc) · 1.09 KB

javascript-error.md

File metadata and controls

43 lines (31 loc) · 1.09 KB

Errors

Reporting abnormal scenarios by throwing errors

If your program encounter a situation that it cannot handle, you can throw errors to report the situation.

In the example below, an error is thrown when the given divider is zero.

function divide(dividend, divisor) {
  if (divisor === 0) {
    throw new Error("The divisor cannot be zero");
  }
  // continue to check other cases to handle
}

Handling errors with catch

When you call a function that may throw errors, and if you have a proper way to handle those errors, then you can choose to catch some of them.

try {
  loadConfiguratoniFile(filePath);
} catch (error) {
  if (error.message === "file not found") {
    // use default configuration
    return defaultConfiguration;
  } else {
    // re-throw the error if we cannot handle it
    throw error;
  }
}

Recommended Readings

Reference