Skip to content

Instantly share code, notes, and snippets.

@nerdybeast
Last active December 11, 2017 17:39
Show Gist options
  • Save nerdybeast/82066b1fb8725e2532962fb8d12e63a6 to your computer and use it in GitHub Desktop.
Save nerdybeast/82066b1fb8725e2532962fb8d12e63a6 to your computer and use it in GitHub Desktop.
Avoid unnecessary catch blocks
//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