Last active
June 22, 2024 19:23
-
-
Save TwoFistedJustice/f4c1124412192eed763b74820a29dac8 to your computer and use it in GitHub Desktop.
This gist demonstrates how errors bubble up (or not) through the call stack depending on where you place your try-catch blocks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* This gist demonstrates how errors bubble up (or not) through the call stack | |
* depending on where you place your try-catch blocks | |
* | |
* You will get different results by reverse-commenting the two versions of the | |
* 'result' variable in topLevel() | |
* */ | |
const topLevel = function(bool=false){ | |
let name = topLevel.name; | |
try { | |
/* ** | |
* Reverse comment these two lines to get a different result | |
** **/ | |
let result = middleLevel_With_Error_Handling(bool); | |
// let result = middleLevel_Without_Error_Handling(bool); | |
if (!result){ | |
throw new Error(`${name} error`); | |
}else { | |
console.log(`${name} didn't catch the error`) | |
} | |
} | |
catch(error){ | |
console.error(`${error.message} at ${name}`) | |
} | |
finally { | |
console.log(`${topLevel.name} finished the process\n\n`); | |
} | |
}; | |
/* | |
* this has error handling - though it won't make a bit of difference | |
* */ | |
const middleLevel_With_Error_Handling = function(bool=false){ | |
let name = middleLevel_With_Error_Handling.name; | |
try{ | |
let result = bottomLevel(bool); | |
if (!result){ | |
throw new Error(`${name} error`); | |
} else { | |
console.log(`${name} is just a go between`) | |
return result; | |
} | |
} | |
catch(error){ | |
console.error(`${error.message} at ${name}`) | |
} | |
}; | |
/* | |
* no error handling at all here | |
* */ | |
const middleLevel_Without_Error_Handling = function(bool=false){ | |
let name = middleLevel_Without_Error_Handling.name; | |
let result = bottomLevel(bool); | |
if (!result){ | |
throw new Error(`${name} error`); // will never fire | |
} else { | |
console.log(`${name} is just a go between`) | |
return result; | |
} | |
}; | |
/* | |
* a false value throws an error but does not trigger the catch block | |
* a true value also throws an error and triggers the catch block. | |
* */ | |
const bottomLevel = function(bool){ | |
let name = bottomLevel.name; | |
if (!bool){ | |
throw new Error(`${bool}: ${name} error bubbled up`) | |
} else { | |
try { | |
throw new Error(`${bool}: ${name} error`) | |
} | |
catch(error){ | |
console.error(`${error.message} at ${name}`) | |
return bool; | |
} | |
} | |
}; | |
topLevel(false); // false triggers the bottom level to throw an error to the top level | |
topLevel(true); // true triggers the bottomeLevel try-catch block and the top level never sees the error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment