Javascript

Exception Handling in JavaScript

Notes on how to handle exceptions using try-catch statements in JavaScript.

Shou Arisaka
1 min read
Nov 3, 2025

Notes on how to handle exceptions using try-catch statements in JavaScript.

try {
  console.log('hoge') ;
} catch (e) {
  console.log('Could not process.') ;
}

// => hoge

try {
  console.log(undifinedVariable) ;
} catch (e) {
  console.log('Could not process.') ; console.log(e) ;
}

// => Could not process.
// => VM6073:5 ReferenceError: undifinedVariable is not defined
// =>     at <anonymous>:3:15

You can add a finally block to execute code regardless of whether an exception occurs.

To throw an error, use throw.

throw "Oh no! An error seems to have occurred!!";

Share this article

Shou Arisaka Nov 3, 2025

๐Ÿ”— Copy Links