Is there a reason why this try is not caught? #2133
Unanswered
joshua-tso
asked this question in
Q&A
Replies: 2 comments
This comment has been hidden.
This comment has been hidden.
-
What would you expect a catch block to do? The function is already called, and the finally block ensures context is restored properly regardless of if the function throws. Omitting the catch block ensures that the caller receives the exception. It is equivalent to simply rethrowing. This is a common pattern when cleanup needs to be performed regardless of success/failure. try {
doSomething();
} finally {
cleanUp();
} Short working example: // test.js
function main() {
try {
throw new Error("this is a failure");
} finally {
console.log("finally");
}
}
try {
main();
} catch (err) {
console.log("caught by caller", err);
} ➜ code node test.js
finally
caught by caller Error: this is a failure
at main (/Users/daniel.dyla/code/test.js:3:11)
at Object.<anonymous> (/Users/daniel.dyla/code/test.js:10:3)
at Module._compile (internal/modules/cjs/loader.js:1251:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1272:10)
at Module.load (internal/modules/cjs/loader.js:1100:32)
at Function.Module._load (internal/modules/cjs/loader.js:962:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
opentelemetry-js/packages/opentelemetry-context-async-hooks/src/AsyncHooksContextManager.ts
Line 48 in c516264
Beta Was this translation helpful? Give feedback.
All reactions