concept exception in category javascript

appears as: n exception, exception, An exception, exceptions
Secrets of the JavaScript Ninja, Second Edition

This is an excerpt from Manning's book Secrets of the JavaScript Ninja, Second Edition.

  • The newly constructed object is returned as the new operator’s value (with an exception that we’ll get to in short order).
  • Throwing exceptions

    There’s another, slightly less orthodox, way to supply a value to a generator: by throwing an exception. Each iterator, in addition to having a next method, has a throw method that we can use to throw an exception back to the generator. Again, let’s look at a simple example.

    Listing 6.9. Throwing exceptions to generators
    function* NinjaGenerator() { try{ yield "Hattori"; fail("The expected exception didn't occur"); #A } catch(e){ #B assert(e === "Catch this!", "Aha! We caught an exception"); #B } #B }   const ninjaIterator = NinjaGenerator();   const result1 = ninjaIterator.next(); #C assert(result1.value === "Hattori", "We got Hattori"); #C ninjaIterator.throw("Catch this!"); #D #A - This fail shouldn’t be reached. #B - Catches exceptions and tests whether we’ve received the expected exception #C - Pulls one value from the generator #D - Throws an exception to the generator !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!
    Figure 6.4. We can throw exceptions to generators from outside a generator.

    This feature that enables us to throw exceptions back to generators might feel a bit strange at first. Why would we even want to do that? Don’t worry; we won’t keep you in the dark for long. At the end of this chapter, we’ll use this feature to improve asynchronous server-side communication. Just be patient a bit longer.

    Functional Programming in JavaScript

    This is an excerpt from Manning's book Functional Programming in JavaScript.

  • Throwing an exception, unless it’s caught within the same function
  • JavaScript errors can occur in many situations, especially when an application fails to communicate with a server or tries to access properties of a null object. Also, third-party libraries can have functions throw exceptions to signal special error conditions. Hence, we always need to be prepared for the worst and design with failure in mind, instead of letting it become an afterthought and regretting it later. In the imperative world, exceptions are handled via the try-catch idiom.

    JavaScript’s current exception-handling mechanism is geared toward throwing and catching exceptions through the popular try-catch structure present in most modern programming languages:

    try {
       // code that might throw an exception in here
    }
    catch (e) {
       // statements to handle any exceptions
       console.log('ERROR' + e.message);
    }

    The purpose of this structure is to surround a piece of code that you deem to be unsafe. Upon throwing an exception, the JavaScript runtime abruptly halts the program’s execution and creates a stack trace of all function calls leading up to the problematic instruction. As you know, specific details about the error, such as the message, line number, and filename, are populated into an object of type Error and passed into the catch block. The catch block becomes a safe haven so that you can potentially recover your program. For example, recall the findObject and findStudent functions:

    // findObject :: DB, String -> Object
    const findObject = R.curry(function (db, id) {
      const result = find(db, id)
      if(!result) {
         throw new Error('Object with ID [' + id + '] not found');
      }
      return result;
    });
    
    // findStudent :: String -> Student
    const findStudent = findObject(DB('students'));

    Because any of these functions can throw an exception, in practice you would need to enclose them in a try-catch block when calling them:

    Figure 5.1. The try-catch structure invisibly creates a safety box around functions that can throw exceptions. This safety box is materialized into a container.

    5.1.2. Reasons not to throw exceptions in functional programs

    The structured mechanism of throwing and catching exceptions in imperative JavaScript code has many drawbacks and is incompatible with the functional design. Functions that throw exceptions