Syntax

JavaScript Try / Catch Syntax

In JavaScript, try / catch: handles an error without crashing — try { ... } catch (err) { console.error(err); }

Quick Answer

Syntax
try { ... } catch (err) { console.error(err); }
JavaScriptSyntax

Try / Catch

Code example
try { ... } catch (err) { console.error(err); }

Handles an error without crashing.

Learn More

How to try / catch in JavaScript

  1. Open your JavaScript file or editor.
  2. Copy the example, then replace its sample names and values for your code.

JavaScript Try / Catch Syntax

Use try { ... } catch (err) { console.error(err); }.

Related Syntax References

Frequently asked questions

What is the JavaScript syntax for try / catch?

Use this pattern: try { ... } catch (err) { console.error(err); }

Can I copy this JavaScript code example?

Yes. Use Copy code and adapt the example to your program.

Is this JavaScript syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify