Are there any negatives to catching an exception and just logging it? This is like the bare minimum done on every exception catch, but it doesn’t really do anything about the exception.
I do believe this depends on the circumstances e.g. user-facing exception needs a popup etc. but something on the backend which the user doesn’t see, does not. Or maybe there’s more to it than just that.
In general, do the right thing in each given situation. Sometimes, that means just logging the exception. In other cases, you can perform specific, targeted recovery.
If a user has requested that you open a file, for example, and you catch a
FileNotFoundException, it’s appropriate to report this to the user and provide them the opportunity to select a different file. Or if parsing a user-provided string triggers aFormatException(though you’ll often use a TryX pattern to avoid exceptions here), tell the user to enter a numeric string.Often, the best thing to do is not catch the exception at all. If you can’t do anything appropriate with it, let it bubble up to the root of your application stack, where you can log it, or often, just let it crash your application. If you don’t know why you’re seeing an exception, are you really sure your application is healthy, and won’t corrupt data?