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
  • The TRY Block
  • The CATCH Block
  • Illustration
  • Key Takeaways
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 2-Control Flow and Error Handling

try catch

A try-catch block marks a block of statements to try, and specifies one or more responses should an exception be thrown. If an exception is thrown, the try-catch statement catches it.

File Location

We will be working in the following file:

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

The TRY Block

The try block will contain one or more statements. That is, you want the try block to succeed, and if it does not succeed, you want control to pass to the catch block. Let's start by creating a function and putting the try block inside of it:

function tryCatchDemo(a){
    try {
        var upperCaseWord = a.toUpperCase();
        console.log(upperCaseWord);
    }
}

Here we have function that takes in a parameter(we'll talk more about functions and parameters later). Let's call the function right below that last curly:

function tryCatchDemo(a){
    try {
        var upperCaseWord = a.toUpperCase();
        console.log(upperCaseWord);
    }
}
tryCatchDemo("Hello");

The CATCH Block

If any statement within the try block throws an exception, control will shift to the catch block. If no exception is thrown in the try block, the catch block is skipped. So here's some starter code for a catch block.

function tryCatchDemo(a){
    try {
        var upperCaseWord = a.toUpperCase();
        console.log(upperCaseWord);
    }
    catch(err){
        console.log(err);
    }
}
tryCatchDemo("Hello");
tryCatchDemo(7); //2nd call

Notice that when we call this function a second time, it breaks. We can't make a 7 uppercase.

Illustration

Here's a starter illustration of the syntax:

try {
    Block to try
}
catch(err) {
    If the try block throws an exception, this block handles errors
}

Key Takeaways

This was a starter in checking for errors. You don't need to master this at this point. Just know that the try-catch block is a thing.

PreviousswitchNextthrow

Last updated 7 years ago