Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Throwing Your Own Errors

Throwing Your Own Errors

The welcome video’s JavaScript report rejected two rows with the reason ValueError is not defined. You found out why by porting the line of Joan’s Python script that refuses line 6’s refund, -40.00: parse_amount() raises its own ValueError, whose message says what arrived.

  • The port follows the Python line by line. float() becomes Number(), which gives the same number for this refund, but NaN for n/a. raise becomes throw, with the same job: it stops the function and sends the error up to the nearest catch. Where Python calls the class, JavaScript puts the new keyword in front. except becomes catch, which names no type, and err.name gives the error’s type as text.
  • It logs the wrong problem. The port logs ReferenceError - ValueError is not defined, not the refund’s message. The line failed before throw could run: JavaScript has no ValueError, KeyError or IndexError class, and naming a class that doesn’t exist throws a ReferenceError, which plays the part of Python’s NameError here.
  • catch took that error too. Unlike except ValueError, catch takes every error, so it received this one and logged it in place of the refund’s problem. That’s the reason the welcome video’s report gave for two rows: the refund, and the blank amount on line 4, since Number() turns blank text into 0.
  • The fix changes only the class. Error is JavaScript’s general error class, much like Exception in Python. Unlike a bare Exception in Python, a plain Error is normal JavaScript, not a shortcut: most code throws one. Your own ValueError class comes later on the path, with classes.
Language: JavaScript
function parseAmount(text) {
  const amount = Number(text);
  if (amount <= 0) {
    throw new Error(
      `amount must be positive, got ${amount}`,
    );
  }
  return amount;
}

try {
  parseAmount("-40.00");
} catch (err) {
  console.log(err.name, "-", err.message);
}

It logs the refund’s own problem:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The welcome video’s JavaScript report rejected two rows with the reason ValueError is not defined. You’ll find out why by porting the line of Joan’s Python script that refuses line 6’s refund.

00:12 float() raises on its own for text like n/a, but a refund is a number, so the function raises its own ValueError, whose message says what arrived.

00:23 The JavaScript port follows the Python line by line. float() becomes Number(), which gives the same number for this refund, but NaN for n/a.

00:34 raise becomes throw, with the same job: it stops the function and sends the error up to the nearest catch. Where Python calls the class, JavaScript puts the new keyword in front.

00:48 At the bottom, try stays, and except becomes catch, which names no type. The log line prints err.name, the error’s type as text, then its message.

01:00 What does the port log for line 6’s refund? Your turn: pause the video and predict.

01:11 JavaScript logs ReferenceError and ValueError is not defined, not the refund’s message.

01:20 The line failed before throw could run. JavaScript has no ValueError, KeyError or IndexError class. Naming a class that doesn’t exist throws a ReferenceError, which plays the part of Python’s NameError here.

01:34 But unlike except ValueError, catch takes every error, so it received this one and logged it in place of the refund’s problem. That’s the reason the welcome video’s report gave for two rows: the refund, and the blank amount on line 4, since Number() turns blank text into zero.

01:54 The fix changes only the class, to Error, JavaScript’s general error class, much like Exception in Python. Run it again, and JavaScript logs Error, then the refund’s message.

02:07 Unlike a bare Exception in Python, a plain Error is normal JavaScript, not a shortcut: most code throws one. Your own ValueError class comes later on the path, with classes.

02:20 So raise becomes throw, but not every Python class exists in JavaScript: there’s no ValueError, so throw an Error whose message says what was expected and what arrived.

02:32 Next up: Choosing an Error and Its Message, a text lesson. You’ll look inside a thrown error, pick the type that fits a bad amount, and see how to make the n/a amounts fail where Joan can see them.

Become a Member to join the conversation.