JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • File Location
  • Sample Code
  • Key Takeaways
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 2-Control Flow and Error Handling

throw

The throw statement throws an exception. The execution of the code will stop, and the code will move on to the next catch block. If no catch block exists among caller functions, the program will stop running.

File Location

We will be working in the following file:

    javascript-library
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
                03-throw.js <----You will be working in this file.

Sample Code

So, we're looking ahead here with functions, but that's ok. We just want you to get the idea here. Let's look at a function:

function addNumbers(a, b) {
    if (isNaN(a) || isNaN(b)) {
      throw "One of the parameters is not a number";
    } else {
        return a + b;
    }
}

So in this function we are going to check if the numbers entered are numbers or not. We do this with isNaN, which means is Not a Number. If one of the numbers is not a number we'll throw an exception.

Just below the function, we'll add the try-catch block:

function addNumbers(a, b) {
    if (isNaN(a) || isNaN(b)) {
      throw "One of the parameters is not a number";
    } else {
        return a + b;
    }
}

// try-catch
try {
  addNumbers(10, 'kenn');
}
catch(error) {
  console.log(error);
}

Now, when we run this, it's going to throw an error because the second parameter kenn is not a number. So the if condition is true in the function, which means we throw the exception.

Key Takeaways

Here we look ahead at functions. Some of this will make more sense when we get there. For now, you should know about the throw keyword. It allows you to take control over exceptions. We call this exception handling.

Previoustry catchNext3-Loops

Last updated 7 years ago