[JavaScript] try-catch-finally executes logic, and whether it includes the impact of return

try {Block} -catch statement

try catchThe statement will first execute the code block wrapped by try, and judge whether to enter catchthe code block according to the execution result of the code block.

  • If catchthe code block also throws an exception, it will override trythe exception thrown in the statement
function test (){
    try {
        throw new Error('error1');
    } catch (e) {
        throw new Error('error2');
    }
}
  • If the code block has a return result, the execution result of the statement catchwill be overwritten when an exception is thrown . tryOtherwise, Blockit returns undefined because an exception is thrown. l
function test (){
    try {
        return 0
    } catch (e) {
        return 1
    }
}
test() // 0
function test1 (){
    try {
        throw new Error()
        return 0 // 这里不会执行到
    } catch (e) {
        return 1
    }
}
test1() //1

try {Block} -finally statement

  • If finallythe code block also throws an exception, it will override trythe exception thrown in the statement
function test (){
    try {
        throw new Error('error1');
    } finally {
        throw new Error('error2');
    }
}
test() //'error2'
  • tryfinallyThe code wrapped with and will be executed in sequence, and the returned results B and F will be recorded at the same time. If F has a return value (normal or abnormal return), the result of F will be returned, otherwise the result of B will be returned
function test (){
    try {
        throw new Error()
    } finally {
        return 1
    }
}
test() // 1

function test2 (){
    try {
        return 0
    } finally {

    }
}
test2() // 0

function test3 (){
    try {
        return 0
    } finally {
        return 1
    }
}

test3() // 1

try {Block} catch -finally statement

It is equivalent to the combined execution of try catch and try finally, first execute the processing result of try catch and then merge the result with finally, the exception in finally will cover the exception and return in try/catch

function test() {
    var a = 1;
    try {
        return a;
    } finally {
        ++a;
    }
}
test() // 1

function test1() {
    var a = 1;
    try {
        return a;
    } finally {
        ++a;
        return a;
    }
}
test1() // 2
function test2() {
    var obj = {a:1};
    try {
        return obj;
    } finally {
        ++obj.a;
    }
}
test2().a // 2

function test3() {
    var obj = {a:1};
    try {
        return obj.a;
    } finally {
        ++obj.a;
    }
}
test1() // 1

Summarize:

  • catchexceptions tryoverride exceptions for andreturn
  • catchreturnThe exception of override tryandreturn
  • finallyThe exception of try/catchoverrides the exception of andreturn
  • finallyreturnThe exception of override try/catchandreturn
  • try/catch/finallyThe code in or returnafter will not be executed

Guess you like

Origin blog.csdn.net/qq_38987146/article/details/125314583