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

Browser debugging is a part of every front-end development process, and with this debugging we are better at finding problems and solving them. We’re going to focus on error handling.

try/catch

A try/catch statement is a way for JAVASCRIPT to handle an exception, and it’s very easy to understand, so we’re writing our code inside of a try, and a catch is how we’re displaying an error in our code inside of a try.

    try {
    / / content
    } catch (error){
        console.log(error,'xxx')};Copy the code

If something goes wrong in the try, it jumps out of the catch. Let’s imagine a block of code writing a method like this, which makes it easy for us to find errors, especially when many people are working on the same project.

The finally clause

The finally clause is optional ina try/catch clause. Earlier, we said that if something ina try fails, it will jump out and execute the catch clause.

With the finally clause, if the try content fails, the finally content continues.

If the try element fails, the catch element is executed first and then the finally element is executed. Even a return cannot prevent the finally element from executing.

    try {
        bear // Error not defined
    } catch (error){
        console.log(error,'xxx')}finally {
        console.log('jackson')};Copy the code

It’s kind of like a promise, and I think it makes sense.

Note: When we use the try/catch method, the browser assumes that the error was handled. It’s a lot like @ error masking in PHP. This way only we developers know about the error, not the user.

Eight types of errors

A, the Error

Error is the base type. Other Error types inherit from this type and are mainly used to customize Error types

Second, the InternalError

An error of type InternalError is thrown by the browser when an exception is thrown by the underlying JavaScript engine. For example, too much recursion leads to stack overflow. This type of error is not typically handled in code.

Third, EvalError

An EvalError type error is thrown when an exception occurs using an eval() function. Basically, an error is reported whenever eval() is not called as a function

Four, RangeError

RangeError is raised when a value is out of bounds. For example, if an array is defined with an unsupported length, such as -20. Or if a stop condition is not set for recursion.

Five, the ReferenceError

ReferenceError occurs when an object cannot be found. This error is often caused by accessing variables that do not exist

Six, SyntaxError

Occurs when the string passed to eval() contains a JavaScript syntax error

TypeError frequently occurs when a function parameter is passed without validation

URIError The path is incorrect

Custom throw error throw

Throw is our custom throw error. A throw must have a value, but there is no restriction on what the value is.

    function err(){
        throw new TypeError('I'm Jackson')
    }
    err();
Copy the code

With throw, the code immediately stops and throws the value we passed in. We can define custom error types like in my code.