This is the second day of my participation in the August More Text Challenge

To do a good job, he must sharpen his tools

Let’s start with throw

  • throwStatement is used to throw aUser-defined exception.
  • Statements after the throw will not be executed.
  • And control is passed to the first catch block in the call stack.
  • If there is no catch block in the caller function, the program terminates.
function getRectArea(width, height) {
  if (isNaN(width) || isNaN(height)) {
    throw 'Parameter is not a number! '; }}try {
  getRectArea(3.'A');
} catch (e) {
  console.error(e);
}

Copy the code

Throw the usage

  • A throw can be followed by data of the primitive type that represents the content of the exception to be thrown.
throw "Error2"; // An exception with a string value was thrown
throw 42;       // An exception was thrown with the integer 42
throw true;     // An exception is thrown with a value of true
Copy the code
  • Throw an object
try {
    throw new Error('Exception object thrown')}catch (error){
    console.log(error, 'Where the variable error is the exception object that was thrown when it was received')}Copy the code

Try catch cognitive

  • A try statement contains a try block consisting of one or more statements and at least one catch block or finally block, or both. The following are the three forms of try declarations:

    • try ... catch
    • try ... finally
    • try ... catch ... finally
  • The code in the catch block means that any statement in the try block (or function called from the try block) throws an exception and it executes. If no exception is thrown in the try block, the catch block is skipped.

  • Finally is executed after a try and catch block, but before the next try declaration.

  • Regardless of whether an exception is thrown or caught,finallyIt’s always executed.

catch

  • The catch block specifies an identifier that holds the value specified by the throw statement.
  • catchThe block is unique because when a catch block is entered, JS creates this identifier and adds it to the current scope.
  • The identifier exists only during the execution of the catch block, and is no longer available after the execution of the catch block.
  • This identifier can be used to get information about the exception being thrown.
try {
    throw new Error('Wrong')}catch (error) {
    // Error is the identifier of the catch block
    console.log(error)
}
Copy the code
  • Any given exception will only leave itRecently,The closed catch blockTo capture a.

finally

  • The finally block is always executed whether or not an exception is thrown.
  • When a try catch block contains a return statement, the code in finally is still executed.
  • If a value is returned from a finally block, that value will be the return value of the entire try-catch-finally block, regardless of whether there are any return statements in the try and catch blocks.

About Exception Catching

  • Try catch fails to catch asynchronous exceptions.
  • To be precise, the only exception that can be caught is when the thread execution has entered the try catch, but the try catch is not finished.
  • For example, if the syntax fails and the code execution does not enter a try catch, the exception cannot be caught
try {
    a. 
} catch(err) {
    console.log('error', err)
}
// Enter this code into the browser console. You get the following error
// Uncaught SyntaxError: Unexpected token '}'
Copy the code

In layman’s terms, your code is not written correctly, how can you catch exceptions??