Debugging: Try...Catch

In this mini-project, you'll practice using try...catch blocks to handle errors in your JavaScript code.

How to debug with try...catch

  1. Identify the code that may throw an error.
  2. Wrap that code in a try block.
  3. Add a catch block to handle any errors that occur.
  4. Optionally, use a finally block to execute code regardless of whether an error occurred.

Example:

            
try {
    // Code that may throw an error
    let result = riskyFunction();
    console.log(result);
} catch (error) {
    // Handle the error
    console.error("An error occurred:", error.message);
} finally {
    // Code that runs regardless of an error
    console.log("Execution completed.");
}
            
        

References:

Debugging Using Browser DevTools

In addition to using try...catch, you can also utilize browser developer tools to debug your code. Here are some tips:

References for Browser DevTools:


Open the Console to View Errors