Last active
December 11, 2017 17:39
-
-
Save nerdybeast/82066b1fb8725e2532962fb8d12e63a6 to your computer and use it in GitHub Desktop.
Avoid unnecessary 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
//Been seeing a lot of this lately in various repos. This is unnecessary because you're simply | |
//rethrowing the same error that would have been thrown anyway thus rendering your try/catch block useless: | |
try { | |
//execute some code.. | |
} catch(error) { | |
throw error; | |
} | |
//The only reason to catch an error is to either handle that error or log it: | |
try { | |
//execute some code.. | |
} catch(error) { | |
loggerInstance.error('Some error occurred performing some operation...', error); | |
throw error; | |
} | |
//or handle the error by returning a value that can be used by the caller: | |
try { | |
//execute some code.. | |
} catch(error) { | |
return someDefaultValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment