Tech Tutorial: Learn to Program with JavaScript – Episode 51: Asynchronous Error Handling

JavaScript objects are created using a very specific notation or syntax called JavaScript Object Notation or JSON for short. JSON is not just used for defining object literals in JavaScript code but is now used as a primary data format for sending and receiving data across the Internet.

In this video, we learn how to handle errors thrown when making asynchronous web requests.


Personal Recommendation (unpaid)
Programming and software development are awesome! Being that you are learning along with me, there is an excellent chance you can make this a career. Based on my own life experience, let me suggest to you that you go get a Computer Science degree from Neumont College of Computer Science and kick your life into hyper-drive.

visit: https://www.neumont.edu/

Tech Tutorial: Learn to Program with JavaScript – Episode 47: Custom Errors

There are times, especially when we are writing code to be consumed by other developers in their code, that we need to provide our own custom errors and we need to throw those errors so that the consuming code can handle them properly. JavaScript provides us the ability to create custom errors of our own design.

In this video, we learn how to create and throw our own custom errors.


Personal Recommendation (unpaid)
Programming and software development are awesome! Being that you are learning along with me, there is an excellent chance you can make this a career. Based on my own life experience, let me suggest to you that you go get a Computer Science degree from Neumont College of Computer Science and kick your life into hyper-drive.

visit: https://www.neumont.edu/

Tech Tutorial: Learn to Program with JavaScript – Episode 46: Error Handling

As with most things in life, crap happens and you do not always get to choose when, how, or where it happens. It is for this reason that programming languages provide structures for handling expected and unexpected errors.

In this video, we learn about the structures JavaScript provides developers for the purpose of handling errors.


Personal Recommendation (unpaid)
Programming and software development are awesome! Being that you are learning along with me, there is an excellent chance you can make this a career. Based on my own life experience, let me suggest to you that you go get a Computer Science degree from Neumont College of Computer Science and kick your life into hyper-drive.

visit: https://www.neumont.edu/

JavaScript: Exception Handling

Exception handling in JavaScript is extremely similar to that in C# or Java. Js utilizes the familiar try-catch blocks to capture exceptions and handle them. You simply wrap code that might throw an exception in the try and then provide a catch to handle the captured exceptions.

Unlike C# and Java, JavaScript does not allow multiple catch blocks nor does it handle capturing exceptions based on specificity. For this you must implement your own code by checking the exception name or using reflection methods to identify the exception types you are after. This is a little cumbersome but doable.

'use strict';

// Exception Handling
//  1. Use 'throw' to create and throw an Exception
//  2. Use the 'name' property to give the Exception an identifiable name
//  3. Use the 'message' property to give the Exception a meaningful message for the user
//  4. Standard Try-Catch as we are used to in Java and C#
//  5. Place code which might create an error in the 'try' block
//  6. Place rescue code in the 'catch' block
//  7. 'try' blocks can only have one 'catch' and one only
//  8. Must check Exception name in order to handle more than one type of Exception at a time

try{
    // Code that can cause an error during execution

    // Code actually throwing an Exception
    throw {
        name: "CustomErrorType",
        message: "I just threw an error at you."
    }
}catch(error){
    if(error.name === "SomeOtherError"){
        // Do something here
    }

    if(error.name === "CustomErrorType"){
        // Code to rescue processing from the error
        console.log("Captured and error: " + error.name);
        console.log("Its message is: " + error.message);
    }
}