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. In this lesson, you found out why, by porting the line in Joan’s Python script that refuses a refund, like line 6 of the October export, -40.00.

For text like n/a, float() raises a ValueError on its own. A refund is a number, though, so parse_amount() raises its own ValueError, with a message that says what arrived. Joan calls it on line 6’s amount and logs the error’s type and its message:

Language: Python
def parse_amount(text):
    amount = float(text)
    if amount <= 0:
        raise ValueError(
            f"amount must be positive, got {amount}"
        )
    return amount

try:
    parse_amount("-40.00")
except ValueError as err:
    print(type(err).__name__, "-", err)

Python prints ValueError - amount must be positive, got -40.0.

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:02 The welcome video’s JavaScript report rejected two rows with the reason ValueError is not defined. In this lesson, you’ll find out why, by porting the line in Joan’s Python script that refuses a refund, like line 6 of the October export.

00:19 For text like n/a, float() raises a ValueError on its own. A refund is a number, though, so the function raises its own ValueError, with a message that says what arrived.

00:32 Joan calls it on line 6’s amount and logs the error’s type and message. Run it, and Python prints the type, then the message. In JavaScript, the port follows the Python line by line.

00:48 The function becomes a function declaration. float() becomes Number(), which gives the same -40 for this refund, though it gives NaN for text like n/a, where float() raises an error.

01:01 raise becomes throw, which does the same job. It stops the function and sends the error out of it, up to the nearest catch block.

01:11 Python creates an object by calling its class. JavaScript puts the new keyword in front of the call, so the port writes new ValueError in its place.

01:22 The f-string message becomes a template literal.

01:27 At the bottom, try stays as it is, and except becomes catch, which takes no type. So one catch block receives every error.

01:39 The log line prints err.name, the error’s type as text, then its message. What an error holds comes next.

01:49 Python logged ValueError and the refund’s message. Your turn: pause the video and predict the line the JavaScript port logs.

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

02:11 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.

02:28 Had Python named a missing class, except ValueError would let that NameError escape. But catch takes every error, so it received this one, and logged it in place of the refund’s problem.

02:42 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.

02:54 The fix keeps throw and changes only the class. Error is JavaScript’s general error class, much like Python’s Exception, so new Error with the same message works.

03:07 Run it again, and JavaScript logs Error, then the refund’s message. It prints -40 where Python prints -40.0, because JavaScript has no separate int and float types.

03:21 Unlike a bare Exception in Python, a plain Error is normal JavaScript, not a shortcut. Most code throws one and lets the message say what was expected and what arrived.

03:34 Your own ValueError class comes later on the path, with classes.

03:40 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.

03:53 Next up: Choosing an Error and Its Message, a text lesson. You’ll look inside a thrown error, pick one of JavaScript’s few error types, and make the n/a amounts that Number() lets through fail where Joan can see them.

Become a Member to join the conversation.