Skip to main content

JavaScript Errors

JavaScript try catch

Written by Published

try runs code that might fail, and catch handles it when it does.

If nothing goes wrong, the catch block never runs.

If something does, execution jumps there immediately.

Example

Example

javascript

let result;

try {
  result = JSON.parse("not json");
} catch (error) {
  result = "could not parse that";
}

console.log(result);

The output is could not parse that.

How try catch Works

Code in the try block runs normally.

If it throws, the rest of the block is skipped.

catch receives the error and decides what to do.

Syntax

Syntax

javascript

try {
  risky();
} catch (error) {
  handle(error);
}

The error parameter can be left out if you do not need it.

The Rest of the try Block Is Skipped

Everything after the failing line is abandoned.

Example

Example

javascript

const steps = [];

try {
  steps.push("before");
  null.length;
  steps.push("after");
} catch (error) {
  steps.push("caught");
}

console.log(steps.join(" "));

The output is before caught.

Keep the try Block Small

A large try block hides which line actually failed.

Wrap only the part that can genuinely throw.

Example

Example

javascript

const text = '{"name":"Ada"}';
let user;

try {
  user = JSON.parse(text);
} catch (error) {
  user = null;
}

console.log(user === null ? "failed" : user.name);

The work that cannot fail sits outside the block.

Variables Declared Inside

A const or let declared in the try block cannot be seen outside it.

Declare it before the block if you need it afterwards.

Example

Example

javascript

let value;

try {
  value = 42;
} catch (error) {
  value = 0;
}

console.log(value);

Declaring inside the block would put it out of reach.

Catching Without the Parameter

If you do not need the error, the brackets can be left off entirely.

Example

Example

javascript

let ok;

try {
  null.length;
  ok = true;
} catch {
  ok = false;
}

console.log(ok);

The output is false.

Do Not Swallow Errors Silently

An empty catch hides the problem completely.

At minimum, record that something failed.

Example

Example

javascript

let status;

try {
  null.length;
} catch (error) {
  status = "failed: " + error.name;
}

console.log(status);

A silent catch is one of the hardest bugs to track down.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript try catch</title>
</head>
<body>

  <h1>try catch</h1>

  <p id="out"></p>

  <script>
    const text = "not valid json";
    const out = document.getElementById("out");

    try {
      const data = JSON.parse(text);
      out.textContent = "Loaded " + data.name;
    } catch (error) {
      out.textContent = "Could not read the data: " + error.name;
    }
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try valid JSON:

Change the text to '{"name":"Ada"}' and see the other branch run.

Important Points

  • try runs code that might fail.
  • catch runs only when something throws.
  • The rest of the try block is skipped on failure.
  • Keep the try block as small as possible.
  • Never leave a catch block completely empty.

Conclusion

try catch is how a program survives something going wrong.

A small try block makes the failure easy to locate.

Silently swallowing errors causes far more trouble than it saves.