I am learning the try...catch
statement in Javascript. While learning I have come to a problem where I am not understanding why the try
block code is not getting executed along with the finally
block when I return the values. Only finally
block gets executed. But when I console the values, the try
block gets executed along with finally
block.
function test() {
try {
return "try block";
} catch (error) {
return "error block";
} finally {
return "finally block";
}
}
console.log(test());
//output -
finally block
function test() {
try {
console.log("try block")
} catch (error) {
console.log("error block", error)
} finally {
console.log("finally block")
}
}
test()
//output -
try block
finally block